java网格布局怎样才会使组件之间紧密贴合
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MiGong extends JFrame{
JFrame f=new JFrame("迷宫");
int a[][]={
{2,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0},
{0,0,1,0,1,1,0,0},
{0,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,1,0,0,0,1,0,1},
{0,1,1,1,1,0,0,1},
{1,1,0,0,0,1,0,1},
{1,1,0,0,0,0,0,3}
};
public static void main(String args[]){MiGong t=new MiGong();}
MiGong(){
Container c=getContentPane();
setLayout(new GridLayout(8,9,50,50));
for(int h=0;h<9;h++){
for(int g=0;g<8;g++){
switch(a[h][g]){
case 0:
JButton p=new JButton();
p.setBackground(Color.red);
add(p);
break;
case 1:
JButton q=new JButton();
q.setBackground(Color.blue);
add(q);
break;
case 2:
JButton m=new JButton("入口");
m.setBackground(Color.green);
add(m);
break;
case 3:
JButton n=new JButton("出口");
n.setBackground(Color.green);
add(n);
break;
}
}
}
setVisible(true);
pack();
setLocation(450,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
编译后怎么成这样了 展开
package aTest;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame {
// JFrame f=new JFrame("迷宫");
int a[][] = { { 2, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 1, 1, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 1 },
{ 0, 1, 1, 1, 1, 0, 0, 1 }, { 1, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 3 } };
test(String title) {
super(title);
// Container c = getContentPane();
// setLayout(new GridLayout(8, 9, 50, 50));
GridLayout grid=new GridLayout(8,9);
grid.setHgap(5);
// grid.setRows(5);
grid.setVgap(5);
setLayout(grid);
for (int h = 0; h < 9; h++) {
for (int g = 0; g < 8; g++) {
switch (a[h][g]) {
case 0:
JButton p = new JButton();
p.setBackground(Color.red);
add(p);
break;
case 1:
JButton q = new JButton();
q.setBackground(Color.blue);
add(q);
break;
case 2:
JButton m = new JButton("入口");
m.setBackground(Color.green);
add(m);
break;
case 3:
JButton n = new JButton("出口");
n.setBackground(Color.green);
add(n);
break;
}
}
}
setVisible(true);
pack();
setLocation(450, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
test t = new test("迷宫");
}
}
设置表格的高度和宽度
2015-01-28