有3个类,
窗口类A中有一个按钮,能开启窗口类B且关闭窗口A!
如何实现?

解决方案 »

  1.   

    Frame a = ...;
    button x = ...
    x.addActionListener(new ActionListener() {
        public void actionPerformed(Event event) {
           YourWindow b = new JFrame(.., a);
           b.setVisible(true); // or b.pack();
        } 
    });
    class YourWindow extends JFrame {
        private Button a;
        public YourWindow(..., Button a) {
           ...;
           this.a = a;
        }
        
        void someMethod() {
           a.setVisible(false); //or a.dispose();
        }
    }
      

  2.   

    YourWindow 中的a应改为JFrame
      

  3.   

    在A的按钮事件中先让A关闭(AFrame.this.dispose()或AFrame.this.SetVisible(false)),再让B可见(BFrame.setVisible(true))。
      

  4.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;class FrameA extends JFrame implements ActionListener{

    JButton button;
    FrameA(String s){
    super(s);
    button = new JButton("Open another window");
    button.setSize(80, 60);
    this.setBounds(100, 100, 400, 300);
    Container mainPane = this.getContentPane();

    mainPane.add(button, BorderLayout.NORTH);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    button.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e){
    JFrame frameB = new JFrame("Frame B");
    frameB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameB.setBounds(200,200, 200, 160);
    frameB.setVisible(true);
    this.dispose();
    }
    }
    public class ThreeFrame { public static void main(String[] args){ FrameA frameA = new FrameA("Frame A");

    }
    }