getText() vs getPassword()(getText() 与 getPassword())
问题描述
我目前正在为一家虚构的公司设计登录系统,现在我只有主登录,需要大量清理.下面是我的登录处理程序.
I'm currently designing a login system for a make-believe company, right now all I have is the Main login, which needs a lot of cleaning up. Below is my login handler.
private class LoginButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(_uid.getText().equalsIgnoreCase("Nathan") && _pwd.getText().equals("password")) {
JOptionPane.showMessageDialog(null, "Congratulations on logging in!");
} else {
JOptionPane.showMessageDialog(null, "Error on login!");
}
}
}
按原样,这工作得很好,但是当我将其更改为
As is, this works perfectly fine, but when I change it to
_pwd.getPassword.equals("password")
当一切输入正确时,它直接指向 else 语句.这里有什么问题?完整程序如下.
it directs straight to the else statement when everything is input correctly. What is wrong here? Full program below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private static final int HEIGHT = 90;
private static final int WIDTH = 400;
JTextField _uid = new JTextField(10);
JPasswordField _pwd = new JPasswordField(10);
JButton _login = new JButton("Login");
JButton _reset = new JButton("Reset");
public Main() {
super("Login - Durptech");
Container pane = getContentPane();
setLayout(new FlowLayout());
add(new JLabel("User ID:"));
add(_uid);
add(new JLabel("Password:"));
add(_pwd);
add(_login);
_login.addActionListener(new LoginButtonHandler());
add(_reset);
_reset.addActionListener(new ResetButtonHandler());
/*if(_uid.getText().equals("") && _pwd.getText().equals("")) {
_login.setEnabled(false);
} else {
_login.setEnabled(true);
}*/
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocation(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class ResetButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
_uid.setText("");
_pwd.setText("");
_uid.requestFocusInWindow();
}
}
private class LoginButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(_uid.getText().equalsIgnoreCase("Nathan") && _pwd.getText().equals("password")) {
JOptionPane.showMessageDialog(null, "Congratulations on logging in!");
} else {
JOptionPane.showMessageDialog(null, "Error on login!");
}
}
}
public static void main(String[] args) {
new Main();
}
}
推荐答案
password.getPassword()
返回一个 char[],而 char[] 不等于 Strings.所以你需要将它与 char[] 进行比较:
password.getPassword()
returns a char[], and char[]'s aren't equal to Strings. So you need to compare it to a char[]:
if (Arrays.equals(password.getPassword(), new char[]{'p','a','s','s','w','o','r','d'}))
这篇关于getText() 与 getPassword()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:getText() 与 getPassword()
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01