在 Java GUI 中给出是/否选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5235871/
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
Give a Yes/No option in a Java GUI
提问by Eric
I'd like to change the code below to present a yes or no option when a user clicks on 'X' but I'm afraid my java newbie skills don't stretch to it yet. Any suggestions please? I'd like to keep the code below as intact as possible in order to see what needs to be done differently for future reference.
我想更改下面的代码以在用户单击“X”时显示是或否选项,但恐怕我的 Java 新手技能还没有达到。请问有什么建议吗?我想保持下面的代码尽可能完整,以便查看需要做哪些不同的工作以供将来参考。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WindowExit extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog( null, "Are you sure you want to close?" );
System.exit(0);
}
}
回答by Saurabh
Use showConfirmDialog as follows:
使用 showConfirmDialog 如下:
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
回答by Petar Minchev
回答by willcodejavaforfood
public static int showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
With an optionType
of JOptionPane.YES_NO_OPTION
随着optionType
中JOptionPane.YES_NO_OPTION