我使用Applet布局,写了两个组件,JTextAreah和JTextField,JTextField能看见而JTextAreah不能看见,而且Applet实际大小能改变,但是组件在里面的位置大小没办法改变,我设布局为空了的,源代码在下面,希望大家帮看看
public class MyApplet extends Applet {
private JTextArea textArea = null;//运行起来看不到JTextArea 
private JTextField text = null;
private JTextArea getJTextArea() {
if (textArea == null) {
textArea = new JTextArea();
textArea.setBackground(Color.green);
textArea.setRows(20);
textArea.setColumns(20);
textArea.setLocation(30, 40);
textArea.setLayout(new FlowLayout(FlowLayout.LEFT));
}
return textArea;
}
private JTextField getJTextField() {
if (text == null) {
text = new JTextField();
text.setBounds(new Rectangle(20, 180,250, 30));
text.setLayout(new FlowLayout(FlowLayout.LEFT));
}
return text;
}
public void init() {
add(getJTextArea());
add(getJTextField());
setSize(600,650); //这大小已经设置了
setLayout(null);
}
}

解决方案 »

  1.   

    你已经将布局管理器设置为null了,
    所有的组件都要手动设置大小及位置
    像这个一样text.setBounds(new Rectangle(20, 180,250, 30)); 
      

  2.   

    import javax.swing.*;
    import java.awt.*;
    import java.applet.Applet;
    public class MyApplet extends Applet { 
    private JTextArea textArea = null;
    private JTextField text = null; 
    private JTextArea getJTextArea() { 
    if (textArea == null) { 
    textArea = new JTextArea(); 
    textArea.setBackground(Color.green); 
    textArea.setRows(20); 
    textArea.setColumns(20); 
    textArea.setLocation(30, 40); 
    textArea.setLayout(new FlowLayout(FlowLayout.LEFT)); 

    return textArea; 

    private JTextField getJTextField() { 
    if (text == null) { 
    text = new JTextField(); 
    //text.setBounds(new Rectangle(20, 180,250, 30)); 
    text.setLayout(new FlowLayout(FlowLayout.LEFT)); 

    return text; 

    public void init() { 
    add(getJTextArea()); 
    add(getJTextField());
    setSize(600,650);  
    setLayout(new FlowLayout()); 


      

  3.   

    一般在getContentPane()上放置其他组件。
    设置一个布局,如BorderLayout或者绝对布局或者网格包布局都可以,默认好像是流式布局。
      

  4.   

    添加textArea.setSize(width, height);