渲染 Java JPasswordField? [英] Rendering Java JPasswordField?

查看:20
本文介绍了渲染 Java JPasswordField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到实际呈现 JPassword 字段的方法.也许渲染不是正确的词,所以这里是交易:

I am trying to find the method that actually renders the JPassword field. Maybe render is not the right word, so here is the deal:

我试图让 JPassword 字段显示不同数量的字符,而不是与我输入的实际密码长度相同.例如,如果我输入 123456 作为密码并且 setEchoChar((Character)value) 为 "#"

I am trying to make the JPassword field show a different number of characters instead of the same length as the actual password i am typing. For example, if i type 123456 for the password and setEchoChar((Character)value) to "#"

密码显示如下:#######
我希望能够生成随机数量的星星来显示:##########
例如,如果数字是 10.(当然没有空格)

The password would show like this: # # # # # #
I want to be able to generate a random number of stars to show: # # # # # # # # # #
if the number was 10 for example. (Without the spaces of course)

我可以通过添加几个监听器来检测更改然后获取文本并用我创建的掩码替换它来做到这一点,但这并不令人满意,我希望能够改变实际的行为目的.一点挑战不会有坏处,对吧?:)我准备好接受任何建议.谢谢.

I was able to do it by just adding a couple of listeners to detect changes then take the text and replace it with a mask that i create, but thats not satisfying, I want to be able to change the behavior of the actual object. A little challenge won't hurt, right? :) I am ready for any suggestions. Thank you.

推荐答案

我想我找到了一种正确"解决方案的方法,尽管我现在没有设法实现它.以下是提示:

I think I found a way to "right" solution although I did not manage to implement it now. Here are the tips:

回显字符被 PasswordView.drawEchoCharacter() 淹没.这是受保护的方法.它可以被覆盖,因此您可以根据需要绘制任意数量的字符.PasswordView 的实例由 BasicPassworFieldUI.create(Element) 创建.BasicPassworFieldUI 的实例由 JComponent.setUI() 分配.

the echo character is drown by PasswordView.drawEchoCharacter(). This is protected method. It can be overridden, so you can draw as many characters as you want. The instance of PasswordView is created by BasicPassworFieldUI.create(Element). Instance of BasicPassworFieldUI is assigned by JComponent.setUI().

所以,我建议的方式是:

So, the way I'd suggest is:

  1. 实现 MyPasswordView 扩展 PasswordView 并覆盖 drawEchoCharacter()
  2. 实现 MyPasswordUI 扩展了 BasicPasswordFieldUI,它覆盖了 View create(Element elem) 并创建了 MyPasswordView 的实例.
  3. 覆盖 JPasswordField 的 setUI() 并设置它们的 MyPasswordUI.
  1. Implement MyPasswordView extends PasswordView and overrides drawEchoCharacter()
  2. Implement MyPasswordUI extends BasicPasswordFieldUI that overrides View create(Element elem) and creates instance of MyPasswordView.
  3. Override setUI() of JPasswordField and set their MyPasswordUI.

我实现了所有这些,但尽管调用了我的方法,但它仍然不起作用.我相信错误在于我的 drawEchoCharacter() 的返回值.现在是凌晨 2 点,我不会修复该错误,但我相信您可以.

I implemented all this but it still does not work although my methods are called. I believe that the bug is in return value of my drawEchoCharacter(). It is 2AM now and I will not fix the bug but I believe you can.

祝您好运,并很高兴知道您已成功解决此问题.

I wish you good luck and will be happy to know that you succeeded to fix this.

这是我的代码:

public class TestPassword {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPasswordField p = new JPasswordField() {
            public void setUI(TextUI ui) { 
                super.setUI(new MyPasswordUI());
            }
        };
        //p.setUI(ui)
        //p.setEchoChar('q');
        final Random r = new Random();


        f.add(p);

        f.setSize(100, 100);
        f.setLocation(100, 100);
        f.setVisible(true);
    }


    public static class MyPasswordUI extends BasicPasswordFieldUI {
        public View create(Element elem) {
            return new MyPasswordView(elem);
        }
    }

    public static class MyPasswordView extends PasswordView {
        public MyPasswordView(Element elem) {
            super(elem);
        }
        protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
            super.drawEchoCharacter(g, x, y, c);
            return super.drawEchoCharacter(g, x, y, c);
        }
    }
}

这篇关于渲染 Java JPasswordField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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