在A位置加入JPanel, 设置Layout为Y方向BoxLayout , 在按钮事件中调用panel.add(new JTextField());

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class bean3 extends JFrame implements ActionListener {
      JPanel contentPanel=(JPanel) this.getContentPane();
      JPanel jp=new JPanel();
      JPanel jp1=new JPanel();
      JButton jb=new JButton("add_JTextField");  public bean3() throws Exception {
        super("myFrame");
        this.setSize(800,600);
        this.setResizable(false);
        this.setLocation(this.getToolkit().getScreenSize().width/2-400,this.getToolkit().getScreenSize().height/2-300);
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add("Center",jp);
        jp.setBackground(Color.white);
        jp.setLayout(new BoxLayout(jp,BoxLayout.PAGE_AXIS));
        contentPanel.add("South",jp1);
        jp1.add(jb);
        jb.addActionListener(this);
        this.setVisible(true);
      }  public void actionPerformed(ActionEvent e) {
        if((JButton)e.getSource()==jb) {
          jp.add(new JTextField("",20));
          jp.validate();
        }
      }  public static void main(String[] args) throws Exception {
        new bean3();
      }  protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
    }