如何才能使此密码字段成为焦点? [英] How can I give this password field focus?

查看:133
本文介绍了如何才能使此密码字段成为焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看似微不足道,但我不知道如何在此对话框焦点中提供密码框.

This may seem trivial, but I can't figure out how to give the password box in this dialog focus.

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class PasswordBox {
    @SuppressWarnings("unused")
    public String prompt() {
        JPasswordField pass = new JPasswordField(10);
        int action = JOptionPane.showConfirmDialog(null, pass,"Enter Password",JOptionPane.OK_CANCEL_OPTION); 
        return new String(pass.getPassword());
    }
}

我从其他这样的类中调用它:String tmpPASS = new PasswordBox().prompt();

I invoke it from other classes like this: String tmpPASS = new PasswordBox().prompt();

由于某种原因,当对话框出现时,确定"按钮将成为焦点.

For some reason, when the dialog shows up, the "OK" button gets focus.

stacktrace(请参阅Enf.Fouad的答案):

stacktrace (see Eng.Fouad's answer):

at javax.swing.JComponent.addNotify(Unknown Source)
at PasswordBox$1.addNotify(PasswordBox.java:14)
at java.awt.Container.addNotify(Unknown Source)

推荐答案

查看对话焦点.

使用Eng Fouad建议的方法,我相信代码应该是:

Using the approach suggested by Eng Fouad I believe the code should be:

JPasswordField pass = new JPasswordField(10)         
{
    public void addNotify()             
    {                 
        super.addNotify();
        requestFocusInWindow();             
    }         
}; 

Edit2:

"Dialog Focus"博客条目中的链接带有注释,并带有在Linux上可用的建议.

The link in the "Dialog Focus" blog entry has a comment with a suggestion that works on Linux.

这篇关于如何才能使此密码字段成为焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆