如何在Jpasswordfield中添加眼睛符号?

发布于 2025-01-28 14:47:30 字数 38 浏览 2 评论 0原文

如何使用Java Swing在Java应用程序中添加眼睛符号?

How can I add an eye symbol in my java application using java swing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2025-02-04 14:47:30

您可以实现多种方式。在以下代码中,我创建a jpanel 包含a 和a jlabel> jlabel 显示“ EYE” ICON。我删除 border> border 来自代码>。 (请注意,默认情况下,jlabel没有边框。)我在jpanel中添加了一个边框,使其看起来像一个单个文本字段。我从接受的答案中得到了这个想法在Jtextfield(Java)内。最后,我添加a mouselisteristener。当鼠标指针在jlabel内部时,jpasswordfield中的文本是可读的,并且鼠标指针不在jlabel中时,文本在jpasswordfield中显示为星号字符串,即*

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.BevelBorder;

public class PassWord extends MouseAdapter {
    private JFrame frame;
    private JPasswordField  passwordField;

    public void mouseEntered(MouseEvent event) {
        passwordField.setEchoChar('\u0000');
    }

    public void mouseExited(MouseEvent event) {
        passwordField.setEchoChar('*');
    }

    private void buildAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createEye(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createEye() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        passwordField = new JPasswordField(15);
        passwordField.setBorder(null);
        panel.add(passwordField);
        URL location = getClass().getResource("eye_show_password.png");
        Icon ico = new ImageIcon(location);
        JLabel label = new JLabel(ico);
        label.setOpaque(true);
        label.setBackground(passwordField.getBackground());
        label.addMouseListener(this);
        panel.add(label);
        JPanel container = new JPanel();
        container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        container.add(panel);
        return container;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> new PassWord().buildAndDisplayGui());
    }
}

You can implement this several ways. In the below code, I create a JPanel which contains a JPasswordField and a JLabel which displays an "eye" icon. I remove the border from the JPasswordField. (Note that, by default, a JLabel does not have a border.) And I add a border to the JPanel that makes it look like a single, text field. I got that idea from the accepted answer to How to put a JButton inside a JTextField (Java). Finally, I add a MouseListener to the JLabel. When the mouse pointer is inside the JLabel, the text in the JPasswordField is readable and when the mouse pointer is not inside the JLabel, the text in the JPasswordField is displayed as a string of asterisk characters, i.e. *.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.BevelBorder;

public class PassWord extends MouseAdapter {
    private JFrame frame;
    private JPasswordField  passwordField;

    public void mouseEntered(MouseEvent event) {
        passwordField.setEchoChar('\u0000');
    }

    public void mouseExited(MouseEvent event) {
        passwordField.setEchoChar('*');
    }

    private void buildAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createEye(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createEye() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        passwordField = new JPasswordField(15);
        passwordField.setBorder(null);
        panel.add(passwordField);
        URL location = getClass().getResource("eye_show_password.png");
        Icon ico = new ImageIcon(location);
        JLabel label = new JLabel(ico);
        label.setOpaque(true);
        label.setBackground(passwordField.getBackground());
        label.addMouseListener(this);
        panel.add(label);
        JPanel container = new JPanel();
        container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        container.add(panel);
        return container;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> new PassWord().buildAndDisplayGui());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文