我写了一个窗体 ,在窗体中加入一个按钮,当我按下这个按钮的同时。在这个窗体上出现一个文本框,应该怎么实现呀??

解决方案 »

  1.   

    field=new JTextField(20);
    先field.setVisible(false);
    按下按钮后field.setVisible(true);
      

  2.   

    Container.validate();
    public void button_actionPerformed(Event e) {
       JTextField tf = new JTextField();
        
       panel.add(tf);
       panel.validate();
    }
      

  3.   

    完整代码:
    import java.awt.*;
    import java.awt.event.*;import javax.swing.*;public class SimpleTest extends JFrame{
    private JButton button;
    private JTextArea textArea;
    private JPanel panel;

    public SimpleTest(){
    super("Button Test");

    button = new JButton("Add Text Area");
    textArea = new JTextArea();

    panel = new JPanel(new GridLayout(2, 1));

    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    panel.add(textArea);
    validate();
    }
    });

    panel.add(button);

    add(panel, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
    }

    public static void main(String[] args) { 
    new SimpleTest();
    }
    }