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);
}
}
}