Java Swing adding Action Listener for EXIT_ON_CLOSE

前端 未结 4 1540
孤城傲影
孤城傲影 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:41

    Window Events: There is the complete program, hope it will help you. public class FirstGUIApplication {

    public static void main(String[] args) {
        //Frame
        JFrame window = new JFrame();
        //Title:setTitle()
        window.setTitle("First GUI App");
        //Size: setSize(width, height)
        window.setSize(600, 300);
        //Show: setVisible()
        window.setVisible(true);
        //Close
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.addWindowListener(new WindowAdapter() {
    
    
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e); 
                JOptionPane.showConfirmDialog(null,"Are sure to close!");
            }
    
            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e); 
                JOptionPane.showMessageDialog(null, "Welcome to the System");
            }
    
        });
    
    }
    

    }

提交回复
热议问题