现有两个窗口,想要点击第一个窗口中的确定按钮打开第二个窗口,请问该用什么事件,如何实现呢?

解决方案 »

  1.   

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;public class Test extends JFrame {

    private JButton button = null;

    private int i = 1; public Test() {
    button = new JButton("打开新窗口");
    button.addActionListener(new ActionListener(){
    JFrame frame = null;
    public void actionPerformed(ActionEvent e) {
    frame = new JFrame("New JFrame " + i++);
    frame.setSize(220, 120);
    frame.setVisible(true);
    }});
    JPanel pane = new JPanel();
    pane.add(button);
    this.getContentPane().add(pane);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.setVisible(true);
    } public static void main(String[] args) {
    new Test();
    }
    }
      

  2.   

    先谢谢了!
      不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法?
      

  3.   

    先谢谢了!
      不过我是定义了两个独立的JFrame,想通过点击第一个JFrame中的确定按钮,来显示第二个JFrame,第一个JFrame被覆盖,不知道有没有其它的方法?
      

  4.   

    第一个被覆盖的话可以在Frame2.show()前把Frame1隐藏起来,hide()方法,然后用判断第二个Frame2是不是closing或closed~~如果是,Frame1在显示出来。
      

  5.   

    既然要覆盖,何必用两个JFrame
    以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要
    import javax.swing.*;
    import java.awt.event.*;public class NewWindow extends JFrame{
        
        private JButton b1 = new JButton("更换窗口");
        private JPanel p1 = new JPanel();
        
        private JButton b2 = new JButton("窗口已更新");
        private JPanel p2 = new JPanel();
        private JPanel panel = (JPanel)this.getContentPane();
        
       public NewWindow(){
           p1.add(b1);
           p2.add(b2);
           
           panel.add(p1);
           b1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   p1.setVisible(false);
                   panel.add(p2);
               }
           } );
       }
       
       public static void main(String[] args) {
        NewWindow w = new NewWindow();
        w.setSize(300,200);
        w.setVisible(true);
    }
    }
      

  6.   

    既然要覆盖,何必用两个JFrame
    以下代码可以实现这切换,在一个JFrame中操作,不知能否满足需要
    import javax.swing.*;
    import java.awt.event.*;public class NewWindow extends JFrame{
        
        private JButton b1 = new JButton("更换窗口");
        private JPanel p1 = new JPanel();
        
        private JButton b2 = new JButton("窗口已更新");
        private JPanel p2 = new JPanel();
        private JPanel panel = (JPanel)this.getContentPane();
        
       public NewWindow(){
           p1.add(b1);
           p2.add(b2);
           
           panel.add(p1);
           b1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   p1.setVisible(false);
                   panel.add(p2);
               }
           } );
       }
       
       public static void main(String[] args) {
        NewWindow w = new NewWindow();
        w.setSize(300,200);
        w.setVisible(true);
    }
    }
      

  7.   

    发一个回复,为什么会出现两个,bug??
      

  8.   

    谢谢各位了,我决定用bitzhq(虹墙)的方法来实现了:)