创建一个登录表单并将密码从hide更改为visible

时间:2018-04-13 22:26:39

标签: java visible jpasswordfield

我试图创建一个登录表单,但在获取showPassword checkBox工作时遇到了一些问题。当选择showPassword时,我想要显示JPasswordField的内容,passwordField,并且当未选择showPassword时,我希望它是"隐藏"。我不明白为什么我的代码不起作用。我以这种方式编写代码,因为我希望将来作为模型视图控制器实现它。如果可能的话,我不希望在公共场合更改私人的任何属性。任何想法为什么这不起作用?谢谢!

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);
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

new Controller(logIn);