Java Swing adding Action Listener for EXIT_ON_CLOSE

前端 未结 4 1529
孤城傲影
孤城傲影 2020-11-28 14:10

I have a simple GUI:

    public class MyGUI extends JFrame{

        public MyGUI(){
           run();
        }

        void run(){
           setSize(100,         


        
4条回答
  •  孤独总比滥情好
    2020-11-28 14:39

    Write this code within constructor of your JFrame:

    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent e) {
            System.out.println("Uncomment following to open another window!");
            //MainPage m = new MainPage();
            //m.setVisible(true);
            e.getWindow().dispose();
            System.out.println("JFrame Closed!");
        }
    });
    

提交回复
热议问题