gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:12:19 24 4
gpt4 key购买 nike

我正在尝试创建登录表单,但在使 showPassword 复选框正常工作时遇到一些问题。当选择 showPassword 时,我希望 JPasswordField、passwordField 的内容可见,当未选择 showPassword 时,我希望它“隐藏”。我不明白为什么我的代码不起作用。我这样编写代码是因为我想将来将其实现为 Model View Controller 。如果可能的话,我不想在公共(public)场合更改任何私有(private)属性。有什么想法为什么这行不通吗?谢谢!

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 的实例,因此永远不会调用该类的构造函数。因此,永远不会调用 showPasswordWhenClicked

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

new Controller(logIn);

关于java - 创建登录表单并将密码从隐藏更改为可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com