java - JOptionPane.showOptionDialog 不显示按钮?

标签 java swing joptionpane

以下代码显示了预期的对话框,但没有按钮:

  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 image description here

为什么没有确定/取消按钮? (顺便说一句,对话框不可调整大小,所以我不知道它们是否位于可见框架之外)

(此外,按 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/

相关文章:

Java:ObjectOutputStream 存在可序列化问题

java - 需要澄清java数组声明

java - 在jframe上动态切换图像

java - Action 监听器似乎没有被触发

java - 随着字符串长度的增加自动更改 Swing 组件中的字体大小

Java向JOptionPane添加组件

java - 当我使用 JButtons 将静态添加到数组列表时,CardLayout 不显示 JButtons

java - java中是否可以有一个无符号的ByteBuffer?

Java - JOptionPane 密码自动保存

java - 使用 JOptionPane 显示容纳的数组