要重新编译才能看出变化!
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
编译是指:javac LottoMadness吧?!
我把GridLayout()的第二个参数改变为其他的数字都没有任何变化,这是为什么?
比如:你定义了一个3行2列的布局,而你加入了9个组件
则,你的布局会自动扩充成3行3列
import javax.swing.*;public class Lotto extends JFrame
{
JLabel numbersLabel =new JLabel("Your picks: ",JLabel.RIGHT);
JTextField[] numbers=new JTextField[6];
JLabel winnersLabel=new JLabel("Winners: ", JLabel.RIGHT);
JTextField[] winners=new JTextField[6];
public Lotto()
{
super("Lotto ");
setSize(550,270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout=new GridLayout(2,7,10,10);
Container pane=getContentPane();
pane.setLayout(layout);
pane.setLayout(layout);
//row2.setLayout(layout2);
pane.add(numbersLabel);
for (int i=0;i<6;i++)
{
numbers[i]=new JTextField();
pane.add(numbers[i]);
} pane.add(winnersLabel);
for(int i=0;i<6;i++)
{
winners[i]=new JTextField();
winners[i].setEditable(false);
pane.add(winners[i]);
}
setVisible(true);
setContentPane(pane);
}
public static void main(String[] arguments)
{
Lotto lo=new Lotto();
}
}我现在越来越不明白了,我把GridLayout layout=new GridLayout(2,7,10,10);中GridLayout()中的前两个参数更改,发现特别有意思的情况出现了。
我把改成GridLayout layout=new GridLayout(5,1,10,10);
他运行时出现的是5行3列。
如果我把它改成GridLayout layout=new GridLayout(8,1,10,10);
他运行时出现的是7行2列。
如果我把它改成GridLayout layout=new GridLayout(14,1,10,10);
他运行时出现的是14行1列。
这到底是怎么回事呢?
难道是由它本身决定的吗?
你总共添加了14个组件
2行7列时,刚好,不出现任何问题
5行1列时,不够,至少要扩充到3列,所以是5行3列
8行1列时,不够,扩充到2列,因为加入组件是逐行添加的(从先添加的是editable的textfield,后添加的是uneditable的textfield可以看出),所以第8行没有组件可加了
这是出现的是8行2列,第8行存在,只是没有组件,你误认为只有7行
14行1列,当然也正常
你能給我解釋一下﹐
GridLayout()的定義嗎﹖
謝謝﹗