Dan*_*man 11 java swing joptionpane
我想将OK和CANCEL按钮的文本设置JOptionPane.showInputDialog
为我自己的字符串.
有一种方法可以更改按钮的文本JOptionPane.showOptionDialog,但我找不到改变它的方法showInputDialog.
小智 18
如果您不想仅使用单个inputDialog,请在创建对话框之前添加这些行
UIManager.put("OptionPane.cancelButtonText", "nope");
UIManager.put("OptionPane.okButtonText", "yup");
Run Code Online (Sandbox Code Playgroud)
其中'是'和'不'是你想要显示的文字
下面的代码应该出现一个对话框,你可以在中指定按钮文本Object[].
Object[] choices = {"One", "Two"};
Object defaultChoice = choices[0];
JOptionPane.showOptionDialog(this,
"Select one of the values",
"Title message",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
defaultChoice);
Run Code Online (Sandbox Code Playgroud)
另外,请务必查看Oracle站点上的Java教程.我在教程http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#create中找到了此链接的解决方案
如果您希望JOptionPane.showInputDialog具有自定义按钮文本,您可以扩展JOptionPane:
public class JEnhancedOptionPane extends JOptionPane {
public static String showInputDialog(final Object message, final Object[] options)
throws HeadlessException {
final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE,
OK_CANCEL_OPTION, null,
options, null);
pane.setWantsInput(true);
pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
pane.setMessageType(QUESTION_MESSAGE);
pane.selectInitialValue();
final String title = UIManager.getString("OptionPane.inputDialogTitle", null);
final JDialog dialog = pane.createDialog(null, title);
dialog.setVisible(true);
dialog.dispose();
final Object value = pane.getInputValue();
return (value == UNINITIALIZED_VALUE) ? null : (String) value;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样称呼它:
JEnhancedOptionPane.showInputDialog("Number:", new Object[]{"Yes", "No"});
Run Code Online (Sandbox Code Playgroud)