//test.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class test{
public static void main(String args[]){
JFrame frame;
JPanel panel;
JButton[][]button;
//JButton close;
GridLayout gl;
panel=new JPanel();
frame=new JFrame("aaa");
frame.setVisible(true);
frame.getContentPane().add(panel);
gl=new GridLayout(2,5);
panel.setLayout(gl);
button=new JButton[2][5];
for(int x=0;x<2;x++)
for(int y=0;y<5;y++){
button[x][y]=new JButton("o");
panel.add(button[x][y]);
}
//close=new JButton("close");
//panel.add(close);
//close.addActionListener(this);
frame.pack();
}
}
应该是: gl=new GridLayout(2,5);
http://www.csdn.net/expert/topic/1004/1004416.xml?temp=.9450647
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number or rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, then they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.
也就是说行是优先考虑的因素。
http://www.csdn.net/expert/topic/1004/1004416.xml?temp=.9450647
//test.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class test{
public static void main(String args[]){
JFrame frame;
JPanel panel;
JButton[][]button;
//JButton close;
GridLayout gl;
panel=new JPanel();
frame=new JFrame();
frame.setVisible(true);
frame.getContentPane().add(panel);
gl=new GridLayout(0,5);
panel.setLayout(gl);
button=new JButton[2][5];
for(int x=0;x<2;x++)
for(int y=0;y<5;y++){
button[x][y]=new JButton("o");
panel.add(button[x][y]);
}
JButton close=new JButton("close");
panel.add(close);
//close.addActionListener(this);
frame.pack();
}
}