java - JOptionpane 文本字段光标不会自动显示
问题描述
我在这里有这个 JOptionPane:
基本上,当它最初打开时,第一个文本字段中没有光标闪烁。我必须物理单击该字段才能开始输入,而不是光标已经在那里,我可以开始输入。我觉得这真的很烦人,我想知道是否有人可以帮助我解决这个问题。
我的 JOptionPane 的代码:
protected JOptionPane getOptionPane(JComponent parent) {
JOptionPane pane = null;
if (!(parent instanceof JOptionPane)) {
pane = getOptionPane((JComponent)parent.getParent());
} else {
pane = (JOptionPane) parent;
}
return pane;
}
public LoginVerify() {
JPasswordField jpf = new JPasswordField(10);
JLabel jl = new JLabel("Password: ");
jl.setFont(new Font("Times New Roman", Font.PLAIN, 15));
JTextField username1 = new JTextField(10);
JLabel label = new JLabel("Username: ");
label.setFont(new Font("Times New Roman", Font.PLAIN, 15));
final JButton okay = new JButton("Ok");
okay.setBackground(Color.WHITE);
okay.setFont(new Font("Times New Roman", Font.PLAIN, 15));
okay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent)e.getSource());
pane.setValue(okay);
}
});
okay.setEnabled(false);
final JButton cancel = new JButton("Cancel");
cancel.setBackground(Color.WHITE);
cancel.setFont(new Font("Times New Roman", Font.PLAIN, 15));
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane pane = getOptionPane((JComponent)e.getSource());
pane.setValue(cancel);
}
});
username1.getDocument().addDocumentListener(new DocumentListener() {
protected void update() {
okay.setEnabled(username1.getText().length() > 0 && jpf.getPassword().length > 0);
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
});
jpf.getDocument().addDocumentListener(new DocumentListener() {
protected void update() {
okay.setEnabled(username1.getText().length() > 0 && jpf.getPassword().length > 0);
}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
update();
}
});
Object[] message = {
label, username1,
jl, jpf,
};
int value = JOptionPane.showOptionDialog(
null,
message,
"LOGIN",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new Object[]{okay, cancel},
okay);
解决方案
推荐阅读
- laravel - Laravel whereIn 在有关系的查询上
- css - 动画剪辑路径会减慢渲染速度吗?
- selenium - Heroku 部署崩溃:selenium 抛出错误 255
- typescript - 启动测试会话失败:错误:无法初始化“@wdio/cucumber-framework”。错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]
- node.js - Cybersource 生产凭证返回身份验证失败,但代码适用于测试凭证
- javascript - passport.deserializeUser 仅在不通过 'getUserById' 传递 'id' 时工作
- flutter - 59:18:错误:没有为类“DatabaseException”定义方法“isDuplicateColumnError”
- html - 在没有主机的情况下将图标添加到 HTML 文件?
- vb.net - 保存 PNG 图像后元数据中的额外字符/字节
- r - 如何修复 hddplot 包安装错误