java JFrame 关闭按钮的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15198549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:48:45  来源:igfitidea点击:

Popup for JFrame close button

javaswingjframewindowlistenerjpopup

提问by Sikander

i am doing some basic Java Swing application(beginner level) . what i have to do is when i press close button on JFrameto colse the window i want a JOptionPane Confirm Dialoginstead of straightforward close

我正在做一些基本的 Java Swing application(初学者级别)。我要做的是当我按下 关闭close button on JFrame窗口时我想要一个 JOptionPane Confirm Dialog而不是直接关闭

here is the code JFrame

这是代码 JFrame

   JFrame  frame= new JFrame("frame"); 
   frame.setSize(300,300);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
   frame.pack();

and JOptionPane code goes like this

和 JOptionPane 代码是这样的

   final JOptionPane optionPane = new JOptionPane("Are You sure?",JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);

so when Close button on JFrame pressed this popup should come up instead of Direct closing
Please guide me how i can do it .. Thanks in advance

所以当 JFrame 上的关闭按钮按下时,这个弹出窗口应该出现而不是直接关闭
请指导我如何做.. 提前致谢

采纳答案by gprathour

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 WindowListenerand 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
           }
     }
    
  1. 更换线frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  2. 实现WindowListener并覆盖其所有抽象方法。你可以在这里找到它

  3. public void windowClosing(WindowEvent e)这种方式覆盖该方法:

     @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
           }
     }
    

回答by Freak

Yes you can do this by using a WindowListener.

是的,您可以使用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 tutorialfor 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 );  
            }  
        }  
    }  
}