Java - 如何为Yes和No选项添加JOptionPane [英] Java - How to add a JOptionPane for Yes and No options
问题描述
所以,我已经阅读了Java API,但似乎仍无法就如何做到这一点做出正面或反面。相信我,我已经尝试过了。我想要一个ActionListener来生成一个带有你真的想要退出吗?文本的消息框,选项是和否退出程序,取决于所选择的按钮。
So, I've read the Java API, but still can't seem to make heads or tails about how to do this. And believe me I have tried. I want an ActionListener to cause a message box with the text 'Do you really want to exit?', with options yes and no which exits the program or not depending on the selected button.
这是我在开始使用消息框打破它之前对ActionListener的所有内容:
Here's what I have for the ActionListener before I started to break it with the message box:
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window.dispose();
}
}
);
如何根据我的要求适当更改?
How can I suitably change it to meet my requirements?
推荐答案
我想你想在你的 ActionListener
中做这样的事情:
I think you want to do something like this inside your ActionListener
:
int selectedOption = JOptionPane.showConfirmDialog(null,
"Do you wanna close the window?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
window.dispose();
}
这篇关于Java - 如何为Yes和No选项添加JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!