以下代码显示了预期的对话框,但没有按钮:
final JPasswordField passwdField = new JPasswordField();
passwdField.setColumns(20);
final JComponent[] inputs = new JComponent[] { passwdField };
int res = JOptionPane.showOptionDialog(null, "Enter Password", "Login",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
null, inputs, "");
显示以下对话框(Java 6.2?、Windows 7 64 位):
为什么没有确定/取消按钮? (顺便说一句,对话框不可调整大小,所以我不知道它们是否位于可见框架之外)
(此外,按 Enter 不会关闭对话框,“x”会关闭对话框)
最佳答案
您的问题出在输入数组上。阅读API,它会告诉你它应该是不同的。我通常使用字符串数组,每个字符串代表一个按钮字符串,或者有时我使用对象的混合物,混合组件和字符串。例如,
JPasswordField passField = new JPasswordField(10);
Object[] inputs = {passField, "OK", "Cancel"};
int res = JOptionPane.showOptionDialog(null, "Enter Password", "Login",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
null, inputs, "");
if (res == 1) {
System.out.println("Password is: " + new String(passField.getPassword()));
}
关于java - JOptionPane.showOptionDialog 不显示按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811119/