简体   繁体   English

用户单击红叉时如何显示确认消息? Java GUI

[英]How do i display a confirmation message when the user clicks on the red cross? Java GUI

I'm working on Java and I'm trying to display a confirmation message to the user when he wants to exit but I didn't know where I have to put it exactly. 我正在使用Java,并且试图在用户想要退出时向用户显示确认消息,但我不知道该把它确切地放在哪里。 may you help me? 你能帮我吗?

You can stop frame from closing by default by using WindowConstants.DO_NOTHING_ON_CLOSE as peram to below API 您可以默认使用WindowConstants.DO_NOTHING_ON_CLOSE作为以下API的权限来阻止框架关闭

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            /* (non-Javadoc)
             * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
             */
            @Override
            public void windowClosing(WindowEvent e) {
                //Use JOptionPane. If everything goes fine 
                                //then do frame.dispose();

            }


        });

If your exit is being done by pressing the x button on the window then you need to handle the windows events. 如果通过按窗口上的x按钮来完成退出,则需要处理Windows事件。

To get the confirmation message you need to pop up a JOptionPane. 要获取确认消息,您需要弹出一个JOptionPane。

There is a discussion of a number of ways to handle the window closing here: 这里讨论了处理关闭窗口的多种方法:

How can a Swing WindowListener veto JFrame closing Swing WindowListener如何关闭JFrame

Documentation on JOptionPane is here: JOptionPane的文档在这里:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

WindowListner添加到您的JFrame并覆盖windowClosing方法,并使用JOptionPane警告用户弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM