java網格
『壹』 用java做個九宮格
定義了一個package名叫aloha
把下面的代碼粘貼了,編譯運行就可以了
不用謝我了!
/*
* NineGrid.java
* @author [email protected]
* Created on 2011-12-20, 13:21:36
*/
package aloha;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
*
* @author li
*/
public class NineGrid extends javax.swing.JFrame implements ActionListener{
/** Creates new form NineGrid */
public NineGrid() {
initComponents();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
this.setSize(400, 400);
this.setTitle("Nine Grid");
this.setLocation((int)(d.getWidth() - 400)/2, (int)(d.getHeight() - 400)/2);
for(int i = 0;i < 15;i++)
{
this.arr[i] = i+1;
}
this.arr[15] = -1;
this.arr[11] = -1;
this.arr[15] = 12;
for(int i = 0;i < 15;i++)
{
int idx =(int) (Math.random() * 15);
int tmp = this.arr[7];
this.arr[7] = this.arr[idx];
this.arr[idx] = tmp;
}
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(this.arr[i * 4 + j] != -1)
{
this.Buttons[i][j] = new JButton("" + this.arr[i * 4 + j]);
this.Buttons[i][j].addActionListener(this);
this.getContentPane().add(this.Buttons[i][j]);
}
else
{
this.Buttons[i][j] = new JButton("");
this.Buttons[i][j].addActionListener(this);
this.getContentPane().add(this.Buttons[i][j]);
this.Buttons[i][j].setEnabled(false);
}
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridLayout(4, 4));
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NineGrid().setVisible(true);
}
});
}
private JButton[][] Buttons = new JButton[4][4];
private int[] arr = new int[16];
private boolean isSucceed()
{
boolean flag = true;
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
if(!this.Buttons[i][j].getText().equals(""))
if(!this.Buttons[i][j].getText().equals(""+(i * 4 + j + 1)))
{
return false;
}
}
}
return true;
}
public void actionPerformed(ActionEvent e)
{
int i = 0,j = 0;
boolean in = false;
for(i = 0;i < 4;i++)
{
for(j = 0;j < 4;j++)
{
if(e.getSource() == this.Buttons[i][j])
{
in = true;
break;
}
}
if(in)
break;
}
if((i >= 0 && (j - 1) >= 0)&&(!this.Buttons[i][j - 1].isEnabled()))
{
String tmp = this.Buttons[i][j].getText();
this.Buttons[i][j].setText(this.Buttons[i][j - 1].getText());
this.Buttons[i][j - 1].setText(tmp);
this.Buttons[i][j - 1].setEnabled(true);
this.Buttons[i][j].setEnabled(false);
if(this.isSucceed())
JOptionPane.showConfirmDialog(this, "You Win!!!!");
return;
}
if((i >= 0 && (j + 1) < 4)&&(!this.Buttons[i][j + 1].isEnabled()))
{
String tmp = this.Buttons[i][j].getText();
this.Buttons[i][j].setText(this.Buttons[i][j + 1].getText());
this.Buttons[i][j + 1].setText(tmp);
this.Buttons[i][j + 1].setEnabled(true);
this.Buttons[i][j].setEnabled(false);
if(this.isSucceed())
JOptionPane.showConfirmDialog(this, "You Win!!!!");
return;
}
if((i - 1 >= 0 && j >= 0)&&(!this.Buttons[i - 1][j].isEnabled()))
{
String tmp = this.Buttons[i][j].getText();
this.Buttons[i][j].setText(this.Buttons[i - 1][j].getText());
this.Buttons[i - 1][j].setText(tmp);
this.Buttons[i - 1][j].setEnabled(true);
this.Buttons[i][j].setEnabled(false);
if(this.isSucceed())
JOptionPane.showConfirmDialog(this, "You Win!!!!");
return;
}
if((i + 1 < 4 && j >= 0)&&(!this.Buttons[i + 1][j].isEnabled()))
{
String tmp = this.Buttons[i][j].getText();
this.Buttons[i][j].setText(this.Buttons[i + 1][j].getText());
this.Buttons[i + 1][j].setText(tmp);
this.Buttons[i + 1][j].setEnabled(true);
this.Buttons[i][j].setEnabled(false);
if(this.isSucceed())
JOptionPane.showConfirmDialog(this, "You Win!!!!");
return;
}
}
// Variables declaration - do not modify
// End of variables declaration
}
『貳』 java中gui網格包布局怎麼設置
GridLayout有三種構造方法。第三種方法可以設計邊距
構造方法詳細信息
1。GridLayout
public GridLayout()
創建具有默認值的網格布局,即每個組件占據一行一列。
從以下版本開始:
JDK1.1
2。GridLayout
public GridLayout(int rows,
int cols)
創建具有指定行數和列數的網格布局。給布局中的所有組件分配相等的大小。
rows 和 cols 中的一個可以為零(但不能兩者同時為零),這表示可以將任何數目的對象置於行或列中。
參數:
rows - 該 rows 具有表示任意行數的值零。
cols - 該 cols 具有表示任意列數的值零。
3。GridLayout
public GridLayout(int rows,
int cols,
int hgap,
int vgap)
創建具有指定行數和列數的網格布局。給布局中的所有組件分配相等的大小。
此外,將水平和垂直間距設置為指定值。水平間距將置於列與列之間。將垂直間距將置於行與行之間。
rows 和 cols 中的一個可以為零(但不能兩者同時為零),這表示可以將任何數目的對象置於行或列中。
所有 GridLayout 構造方法都服從這一規定。
參數:
rows - 該 rows 具有表示任意行數的值零
cols - 該 cols 具有表示任意列數的值零
hgap - 水平間距
vgap - 垂直間距
拋出:
IllegalArgumentException - 如果將 rows 和 cols 的值都設置為零
『叄』 Java網格包布局,組件垂直間距怎麼調
GridBagLayout里的各種設置都必須通過GridBagConstraints,因此當我們將GridBagConstraints的參數都設置
好了之後,必須new一個GridBagConstraints的對象出來,以便GridBagLayout使用。
參數說明:
gridx,gridy:設置組件的位置,gridx設置為GridBagConstraints.RELATIVE代表此組件位於之前所加入組件的右邊。
若將gridy設置為GridBagConstraints.RELATIVE代表此組件位於以前所加入組件的下面。建議定義出gridx,gridy的
位置,以便以後維護程序。表示放在幾行幾列,gridx=0,gridy=0時放在0行0列。
gridwidth,gridheight:用來設置組件所佔的單位長度與高度,默認值皆為1。你可以使用GridBagConstraints.REMAINDER常量,代表此組件為此行或此列的最後一個組件,而且會占據所有剩餘的空間。
weightx,weighty:用來設置窗口變大時,各組件跟著變大的比例,當數字越大,表示組件能得到更多的空間,默認值皆為0。
anchor:當組件空間大於組件本身時,要將組件置於何處,有CENTER(默認值)、NORTH、NORTHEAST、EAST、SOUTHEAST、 WEST、NORTHWEST可供選擇。
insets:設置組件之間彼此的間距,它有四個參數,分別是上,左,下,右,默認為(0,0,0,0).
ipadx,ipady:設置組件內的間距,默認值為0。
『肆』 用JAVA實現網格的客戶端程序
你把網路當成萬能了的了吧~~ 你這個東西坦白的說給我少3W我都不帶理你的
『伍』 用JAVA編寫一個程序繪制一個10*10的網格,每格(正方形)邊長為20像點。
import java.awt.*;
import javax.swing.*;
public class DrawRectTest extends JFrame
{
public DrawRectTest()
{
GPanel g=new GPanel();
this.getContentPane().add(g);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args)
{
new DrawRectTest();
}
}
class GPanel extends JPanel
{
public void paintComponent(Graphics g)
{
int w;//橫坐標
int h;//縱坐標
for(int i=1;i<=10;i++)
{
h=30;
w=10;
for(int j=1;j<=10;j++)
g.drawRect(w=w+20, h, 20, 20);
}
}
}
『陸』 java網格包布局管理器的使用網格包布局的一般步驟
1、創建一個GridBagLayout網格包布局對象,並使其成為當前容器的布局管理器。
2、創建一個GridBagConstraints類型的約束對象,然後使用該對象設置各種約束條件,注意這里設置的約束條件並沒有針對某一組件,要使某一組件具有設置的約束,需要使用第3步驟的方法。
3、然後使用GridBagLayout網格包布局中的setConstraints(Component com, GridBagConstraints cons)方法將GridBagConstraints類型對象設置的約束添加到被設置的組件中,這樣該組件就具有了GridBagConstraints設置的約束。其中setConstraints()的第一個參數是將要添加的組件,第二個參數是GridBagConstraints類型的約束對象。
4、最後將設置了約束的組件添加到容器中。
『柒』 java中有關於網格布局的特點。
Label label = new Label("Hello",Label.CENTER);//AWT中的
JLabel jlabel = new JLabel("Hello",JLabel.CENTER);//swing中的
API中有詳細解釋,多看看。
『捌』 java swing網格布局疑問,求指導!
GridBagConstraints 有幾個成員屬性,你需要把它們賦值,不能 new GridBagConstaints() 就了事了。你需要告訴它,你希望這個組件顯示在 哪個 x, y 位置,x, y 方向上各占幾個格子等。比如,圖片中的樣子,
標題是:x = 2, y = 0, height = 1, width = 2.
用戶名:x = 0, y = 1, height = 1, width = 1.
密碼是:x = 0, y = 2, height = 1, width = 1.
兩個輸入框分別是:
x = 1, y = 1, h = 1, w = 3
x = 1, y = 2, h = 1, w = 3.
這個布局是假設每個方向上都按標准單位來定義尺寸,然後我們給組件指定它在哪個位置每個方向占幾個格子。
『玖』 java 使用網格布局後
你可以格式下,我想你大概要的這種效果,給你代碼可以參考下:
importjava.awt.*;
importjavax.swing.*;
importjava.awt.GridLayout;
@SuppressWarnings("serial")
{
publicstaticvoidmain(String[]args){
JFramejf=newJFrame("注冊");
JPaneljpanel=newJPanel();
jf.setContentPane(jpanel);
//設置窗口在屏幕中居中顯示
doublewidth=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
doubleheight=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
intx=(int)(width-300)/2;
inty=(int)(height-250)/2;
jf.setBounds(x,y,300,150);
//添加標簽組件
GridLayoutgird=newGridLayout(3,0);
JPaneljpanel1=newJPanel();
JPaneljpanel2=newJPanel();
JPaneljpanel3=newJPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
JLabelname=newJLabel("用戶名:");
JTextFieldnametext=newJTextField(10);
JLabelpwd=newJLabel("密碼:");
JPasswordFieldpwd1=newJPasswordField(10);
JLabelpwd2=newJLabel("確認密碼:");
JPasswordFieldpwd3=newJPasswordField(10);
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(pwd2);
jpanel3.add(pwd3);
jf.getLayeredPane().setLayout(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
jf.setVisible(true);
}
}
效果:
滿意請採納!
『拾』 如何用 Java 編寫網格程序
import java.awt.*;
import javax.swing.*;
public class DrawRectTest extends JFrame
{
public DrawRectTest()
{
GPanel g=new GPanel();
this.getContentPane().add(g);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args)
{
new DrawRectTest();
}
}
class GPanel extends JPanel
{
public void paintComponent(Graphics g)
{
int w;//橫坐標
int h;//縱坐標
for(int i=1;i<=10;i++)
{
h=30;
w=10;
for(int j=1;j<=10;j++)
g.drawRect(w=w+20, h, 20, 20);
}
}
}