我使用:
char[] password = null;
JPasswordField jpf = new JPasswordField(30);
java.lang.Object [] messageInput = { prompt, jpf };
java.lang.Object [] options = { jpf , "OK", "Cancel"};
int result = JOptionPane.showOptionDialog(null, messageInput, title,
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, "");
JOptionPane.showMessageDialog(null,
result);
if (result == 1) {
password = jpf.getPassword();
}
else if(result == JOptionPane.CANCEL_OPTION)
{
}
return password;
获取密码,但是这个不能监听回车按钮。 我知道如果我将选项参数设置为空,可以使对话框监听“输入”按钮,但如果我这样做,对话框在显示时不会聚焦于文本框。
有人可以帮我解决这个问题吗?
最佳答案
I know if I set the options parameter to null, can make the dialog listen to "enter" button, but if I do that, the dialog don't focus to the textbox when show up.
Dialog Focus应该可以帮助你。
关于java - 使用 JOptionPane.showOptionDialog 时如何监听 Enter 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4601219/