Popup for JFrame close button

前端 未结 2 642
旧巷少年郎
旧巷少年郎 2021-02-10 22:42

i am doing some basic Java Swing application (beginner level) . what i have to do is when i press close button on JFrame to colse the window i

相关标签:
2条回答
  • 2021-02-10 23:07

    You can do it by following steps:

    1. Replace the line frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); with frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    2. Implement WindowListener and override its all abstract methods. You can find it here.

    3. Override the public void windowClosing(WindowEvent e) method some this way:

       @Override
       public void windowClosing(WindowEvent e){
             int result = JOptionPane.showConfirmDialog(null, "Are you sure,"Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
      
             if(result == JOptionPane.YES_OPTION){
                     System.exit(0);
             }else{
                     //Do nothing
             }
       }
      
    0 讨论(0)
  • 2021-02-10 23:12

    Yes you can do this by using a WindowListener.

     public void windowClosed(WindowEvent e) {
            //This will only be seen on standard output.
            displayMessage("WindowListener method called: windowClosed.");
        }
    
        public void windowOpened(WindowEvent e) {
            displayMessage("WindowListener method called: windowOpened.");
        }
    
        public void windowIconified(WindowEvent e) {
            displayMessage("WindowListener method called: windowIconified.");
        }
    
        public void windowDeiconified(WindowEvent e) {
            displayMessage("WindowListener method called: windowDeiconified.");
        }
    
        public void windowActivated(WindowEvent e) {
            displayMessage("WindowListener method called: windowActivated.");
        }
    
        public void windowDeactivated(WindowEvent e) {
            displayMessage("WindowListener method called: windowDeactivated.");
        }
    
        public void windowGainedFocus(WindowEvent e) {
            displayMessage("WindowFocusListener method called: windowGainedFocus.");
        }
    
        public void windowLostFocus(WindowEvent e) {
            displayMessage("WindowFocusListener method called: windowLostFocus.");
        }
    
        public void windowStateChanged(WindowEvent e) {
            displayStateMessage(
              "WindowStateListener method called: windowStateChanged.", e);
    



    Please see this tutorial for further details.
    But for Your scenario , I recommend you to work with adapter class (as you need only one event so dont need to get tired and implement all methods)so here is an example for according to your requirment

    import java.awt.event.WindowAdapter;  
    import java.awt.event.WindowEvent;  
    import javax.swing.JFrame;  
    import javax.swing.JOptionPane;  
    
    public class NoCloseFrame extends JFrame {  
        public static void main( String[] arg ) {  
            new NoCloseFrame();  
        }  
    
        public NoCloseFrame() {  
            super( "No Close Frame!" );  
            setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );  
            setSize( 300, 300 );  
            setVisible( true );  
            addWindowListener( new AreYouSure() );  
        }  
    
        private class AreYouSure extends WindowAdapter {  
            public void windowClosing( WindowEvent e ) {  
                int option = JOptionPane.showOptionDialog(  
                        NoCloseFrame.this,  
                        "Are you sure you want to quit?",  
                        "Exit Dialog", JOptionPane.YES_NO_OPTION,  
                        JOptionPane.WARNING_MESSAGE, null, null,  
                        null );  
                if( option == JOptionPane.YES_OPTION ) {  
                    System.exit( 0 );  
                }  
            }  
        }  
    }  
    
    0 讨论(0)
提交回复
热议问题