【问题标题】:Popup for JFrame close buttonJFrame 关闭按钮的弹出窗口
【发布时间】:2016-03-11 03:34:13
【问题描述】:

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

这里是代码 JFrame

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

JOptionPane 代码是这样的

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

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

【问题讨论】:

标签: java swing jframe windowlistener jpopup


【解决方案1】:

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



请参阅this tutorial 了解更多详细信息。
但对于您的场景,我建议您使用适配器类(因为您只需要一个事件,所以不需要厌倦并实现所有方法)所以这里是根据您的要求的示例

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 );  
            }  
        }  
    }  
}  

【讨论】:

【解决方案2】:

您可以按照以下步骤进行:

  1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 行替换为frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  2. 实现WindowListener 并覆盖其所有抽象方法。你可以find it here

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

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2011-04-14
    相关资源
    最近更新 更多