简单的方法,有一个混合的JTextField / JPasswordField中? [英] Simple way to have a hybrid JTextField / JPasswordField?

查看:88
本文介绍了简单的方法,有一个混合的JTextField / JPasswordField中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的小程序,有一个simpe登录界面。

I am developing a simple applet that has a simpe sign-in interface.

和是在简洁的空间,我有两个JTextField用户名和密码,这也是我作为标签使用。即下手,用户名的JTextField将是pre-充满了灰色的文字说:用户名和密码的JTextField pre-充满了简单密码。

And to be concise in space, I have two JTextFields for username and password, which I also use as labels. i.e. to start with, the username JTextField will be pre-filled with grey text saying "username" and the password JTextField pre-filled with "simple password".

只要JTextField中获得焦点的话,我清除pre灌注文本和设置文本颜色为黑色。类似计算器的搜索框,但在如火如荼。

Then as soon as the JTextField gets focus, i clear the prefill text and set the text color to black. Similar to stackoverflow's search box, but in swing.

现在为了安全,我想掩盖密码字段当密码JTextField中获得焦点(当然还是有pre填充文字清晰下手)。 JPasswordField中不允许屏蔽/取消屏蔽的切换。

Now for security, I would like to mask the password field when the password JTextField gets focus (but of course still have the pre-filled text legible to start with). JPasswordField doesn't allow the toggling of mask/unmask.

一个简单的方法,我简单的小程序,以获得此功能的任何想法?

Any ideas for a simple way to obtain this functionality in my simple applet?

推荐答案

您可以禁用与setEchoChar((char)的0)的掩蔽回声性质;如在JavaDoc说明。

You can disable the masking echo character with setEchoChar((char)0); as stated in the JavaDoc.

一个例子

    final JPasswordField pass = new JPasswordField("Password");
    Font passFont = user.getFont();
    pass.setFont(passFont.deriveFont(Font.ITALIC));
    pass.setForeground(Color.GRAY);
    pass.setPreferredSize(new Dimension(150, 20));
    pass.setEchoChar((char)0);
    pass.addFocusListener(new FocusListener() {

        public void focusGained(FocusEvent e) {
            pass.setEchoChar('*');
            if (pass.getText().equals("Password")) {
                pass.setText("");
            }
        }

        public void focusLost(FocusEvent e) {
            if ("".equalsIgnoreCase(pass.getText().trim())) {
                pass.setEchoChar((char)0);
                pass.setText("Password");
            }
        }});

格尔茨,
GHAD

Greetz, GHad

这篇关于简单的方法,有一个混合的JTextField / JPasswordField中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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