创建一个登录表单并将密码从隐藏更改为可见

create a login form and change password from hide to visible

我正在尝试创建一个登录表单,但在使 showPassword 复选框正常工作时遇到了一些问题。 When showPassword is selected I want the content of the JPasswordField, passwordField, to be visible and when showPassword is not selected I want it to be "hide".我不明白为什么我的代码不起作用。我这样写代码是因为我想在未来将它实现为一个模型视图控制器。如果可能,我不想更改 public 中的任何私有属性。任何想法为什么这不起作用?谢谢!

package project3;

import javax.swing.JFrame;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class LogInWindow extends JFrame {
    private Container container = getContentPane();
    private JLabel titleLabel = new JLabel("WarehouseApp");
    private JLabel userLabel = new JLabel("USERNAME");
    private JLabel passwordLabel = new JLabel("PASSWORD");
    private JTextField userTextField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();
    private JButton loginButton = new JButton("LOGIN");
    private JCheckBox showPassword = new JCheckBox("Show Password");
    private JLabel logInAsLabel = new JLabel("LOGIN AS");
    private JComboBox<String> logInAsComboBox = new JComboBox<String>();

    public LogInWindow() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(10, 10, 370, 370);
        this.setName("Login Form");
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        container.setLayout(null);

        titleLabel.setBounds(80, -10, 200, 100);
        userLabel.setBounds(50, 80, 100, 30);
        userTextField.setBounds(150, 80, 150, 30);
        passwordLabel.setBounds(50, 130, 100, 30);
        passwordField.setBounds(150, 130, 150, 30);
        logInAsLabel.setBounds(50, 180, 100, 30);
        logInAsComboBox.setBounds(150, 180, 150, 30);
        showPassword.setBounds(150, 220, 150, 30);
        loginButton.setBounds(150, 260, 100, 30);

        Font font = new Font("Times New Roman", Font.BOLD, 30);
        titleLabel.setFont(font);
        logInAsComboBox.addItem("ADMIN");
        logInAsComboBox.addItem("CLIENT");
        logInAsComboBox.setSelectedIndex(-1);

        container.add(titleLabel);
        container.add(userLabel);
        container.add(passwordLabel);
        container.add(userTextField);
        container.add(passwordField);
        container.add(logInAsLabel);
        container.add(logInAsComboBox);
        container.add(showPassword);
        container.add(loginButton);

    }

    public void showPasswordWhenClicked(ActionListener listenForPassword) {
        showPassword.addActionListener(listenForPassword);
    }

    public boolean getPasswordStatus() {
        if (showPassword.isSelected()==true) 
            return true;
        return false;
    }

    public void setPasswordVisible() {
        passwordField.setEchoChar((char) 0);
    }

    public void setPasswordInvisible() {
        passwordField.setEchoChar('*');
    }   
}

package project3;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Controller {

    private LogInWindow theView;

    public Controller(LogInWindow theView) {
        this.theView = theView;

        this.theView.showPasswordWhenClicked(new showPasswordListener());
    }

    public class showPasswordListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            if (theView.getPasswordStatus()==true) {
                theView.setPasswordVisible();
            } else {
                theView.setPasswordInvisible();
            }
        }
    }

    public static void main(String[] args) {
        LogInWindow logIn = new LogInWindow();
        logIn.setVisible(true);
    }
}

您的代码不会创建 Controller 的实例,因此永远不会调用 class 的构造函数。因此,永远不会调用 showPasswordWhenClicked

尝试将此行添加到您的 main 方法中:

new Controller(logIn);