class A extends JFrame{
private TextArea txa=new TextArea();
private TextField txf=new TextField();
public static void main(String[] arges){
new A().initialize();
}
public void initialize(){
this.setLocation(400,300),
setSize(300,300);
add(txa,BorderLayout.NORTH);
add(txf,BorderLayout.SOUTH);
pack()
setVisible(true);}
}
这样的话JFrame大小正好为300*300,但如果我把TextArea改为JTextArea,把TextField改为JTextField,JFrame的大小怎么会变了呢?大侠帮帮我
private TextArea txa=new TextArea();
private TextField txf=new TextField();
public static void main(String[] arges){
new A().initialize();
}
public void initialize(){
this.setLocation(400,300),
setSize(300,300);
add(txa,BorderLayout.NORTH);
add(txf,BorderLayout.SOUTH);
pack()
setVisible(true);}
}
这样的话JFrame大小正好为300*300,但如果我把TextArea改为JTextArea,把TextField改为JTextField,JFrame的大小怎么会变了呢?大侠帮帮我
setSize(300,300); 放在 pack();后面就可以了。
所以在其后面设置大小.
根据PreferredSize容器再去fit 可以看见同一容器和布局及子组件,pack前后PreferredSize都有不同