Java 如何让 JOptionPane.showConfirmDialog 默认没有选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1395707/
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
How to make JOptionPane.showConfirmDialog have No selected by default?
提问by splintor
I implemented a Save As dialog in Java that prompts the user if the file already exists, and I want the No option to be selected by default. How do I do this?
我在 Java 中实现了一个“另存为”对话框,该对话框会提示用户文件是否已存在,并且我希望默认选择“否”选项。我该怎么做呢?
Here is my current code:
这是我当前的代码:
JFileChooser chooser = new JFileChooser()
{
public void approveSelection()
{
File selectedFile = getSelectedFile();
if (selectedFile != null && selectedFile.exists( ) )
{
int response = JOptionPane.showConfirmDialog(
this,
"The file " + selectedFile.getName() + " already exists."
+ " Do you want to replace the existing file?",
getDialogTitle(),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION )
{
return;
}
}
super.approveSelection();
}
};
采纳答案by Vinay Sajip
Use this constructor:
使用这个构造函数:
JOptionPane(Object message, int messageType, int optionType,
Icon icon, Object[] options, Object initialValue)
where optionsspecifies the buttons, and have initialValue(one of the optionsvalues) specify what the default is.
whereoptions指定按钮,并让initialValue(其中一个options值)指定默认值。
Update:You can call showOptionDialograther than showConfirmDialog. The former takes optionsand initialValueparameters.
更新:您可以调用showOptionDialog而不是showConfirmDialog. 前者需要options和initialValue参数。
回答by Carlos Tasada
That's the first thing that comes to my mind.
这是我想到的第一件事。
//Custom button text
Object[] options = {"Yes",
"No"};
JOptionPane.showOptionDialog(this, "The file " + selectedFile.getName() +
" already exists. Do you want to replace the existing file?",
getDialogTitle(),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[1]);
But probably there's a better approach.
但可能有更好的方法。
回答by Koen Weyn
If you don't want to hardcode "Yes" and "No" (for instance when your app is localized for other languages), you can use UIManager resources:
如果您不想硬编码“是”和“否”(例如当您的应用程序针对其他语言进行本地化时),您可以使用 UIManager 资源:
UIManager.getString("OptionPane.yesButtonText", l)
UIManager.getString("OptionPane.noButtonText", l)
回答by Cristian
For the above example, it is JOptionPane.showOptionDialogThose arguments can no be passed to showConfirmDialogbecause it does not have them.
对于上面的示例,JOptionPane.showOptionDialog这些参数不能传递给,showConfirmDialog因为它没有它们。
More people might be looking for this so why not offer a "working" solution.
更多的人可能正在寻找这个,所以为什么不提供一个“有效”的解决方案。
回答by subes
This is my solution:
这是我的解决方案:
import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class NegativeDefaultButtonJOptionPane {
public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) {
List<Object> options = new ArrayList<Object>();
Object defaultOption;
switch(optionType){
case JOptionPane.OK_CANCEL_OPTION:
options.add(UIManager.getString("OptionPane.okButtonText"));
options.add(UIManager.getString("OptionPane.cancelButtonText"));
defaultOption = UIManager.getString("OptionPane.cancelButtonText");
break;
case JOptionPane.YES_NO_OPTION:
options.add(UIManager.getString("OptionPane.yesButtonText"));
options.add(UIManager.getString("OptionPane.noButtonText"));
defaultOption = UIManager.getString("OptionPane.noButtonText");
break;
case JOptionPane.YES_NO_CANCEL_OPTION:
options.add(UIManager.getString("OptionPane.yesButtonText"));
options.add(UIManager.getString("OptionPane.noButtonText"));
options.add(UIManager.getString("OptionPane.cancelButtonText"));
defaultOption = UIManager.getString("OptionPane.cancelButtonText");
break;
default:
throw new IllegalArgumentException("Unknown optionType "+optionType);
}
return JOptionPane.showOptionDialog(parentComponent, message, title, optionType, JOptionPane.QUESTION_MESSAGE, null, options.toArray(), defaultOption);
}
}

