Java 为什么我的JFrame用户名/密码程序声明每次登录都成功?
每当我在用户名/密码字段中输入任何内容时,我总是获得成功。我尝试用一个额外的1来重命名变量,看看问题是不是我意外地使它们彼此相等。我认为情况并非如此。我知道一个简单的程序,但我找不到哪里出错了Java 为什么我的JFrame用户名/密码程序声明每次登录都成功?,java,Java,每当我在用户名/密码字段中输入任何内容时,我总是获得成功。我尝试用一个额外的1来重命名变量,看看问题是不是我意外地使它们彼此相等。我认为情况并非如此。我知道一个简单的程序,但我找不到哪里出错了 package guiPack; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFr
package guiPack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame implements ActionListener {
Container container = getContentPane(); // If the code is part of a JFrame, you must use getContentPane
JLabel userLabel = new JLabel("Username"); // I believe that this part is pretty self-explanatory
JLabel passwordLabel = new JLabel("Password");
JTextField userTextField = new JTextField();
JPasswordField passwordField = new JPasswordField();
JButton loginButton = new JButton("LOGIN");
JCheckBox showPassword = new JCheckBox("Show Password");
JButton rulesButton = new JButton("Rules");
JButton createAccButton = new JButton("Register Account");
LoginFrame() {
setLayoutManager();
setLocationAndSize();
addComponentsToContainer();
addActionEvent();
}
public void setLayoutManager() {
container.setLayout(null);
}
public void setLocationAndSize() {
userLabel.setBounds(50, 150, 100, 30);
passwordLabel.setBounds(50, 220, 100, 30);
userTextField.setBounds(150, 150, 150, 30);
passwordField.setBounds(150, 220, 150, 30);
showPassword.setBounds(150, 250, 150, 30);
loginButton.setBounds(80, 300, 200, 30);
rulesButton.setBounds(130, 50, 100, 30);
createAccButton.setBounds(80, 350, 200, 30);
}
public void addComponentsToContainer() {
container.add(userLabel);
container.add(passwordLabel);
container.add(userTextField);
container.add(passwordField);
container.add(showPassword);
container.add(loginButton);
container.add(rulesButton);
container.add(createAccButton);
}
public void addActionEvent() {
loginButton.addActionListener(this);
showPassword.addActionListener(this);
rulesButton.addActionListener(this);
createAccButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent myactionevent) {
if (myactionevent.getSource() == loginButton) {
String usernameText;
// String passwordText; THIS DOES NOT WORK (PART OF THE BELOW COMMENT CODE)
usernameText = userTextField.getText();
// passwordText = passwordField.getText(); THIS DOES NOT WORK OR HAS A WARNING BECAUSE THE PASSWORD IS STORED AS A STRING AND IS NOT IN MEMORY. ALTERNATIVE IS USING THE BELOW
String passwordText=String.valueOf(passwordField.getPassword());
if (usernameText.equalsIgnoreCase(usernameText) && passwordText.equalsIgnoreCase(passwordText)) // In order to get this part to work you must remove the pre-set password and set it to the string you got from the register below
{
JOptionPane.showMessageDialog(this, "Login Successful");
}
else
{
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
userTextField.setText("");
passwordField.setText("");
}
}
if (myactionevent.getSource() == showPassword) {
if (showPassword.isSelected()) {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*'); //this will hide all the characters in the password field
}
}
if (myactionevent.getSource() == rulesButton) {
// if (rulesButton.isSelected()) THIS DOES NOT WORK!
{
JOptionPane.showMessageDialog(this, "Here are the rules!"
+ "1. If username and password are correct, success!"
+"2. If username and password do not match, incorrect!" + "3. Password must include a special character"); // I am not sure why this is not working. I wanted it to be a row.
}
}
if (myactionevent.getSource() == createAccButton) {
String usernameText = JOptionPane.showInputDialog("Enter a username!"); // Option to input a username
String passwordText = JOptionPane.showInputDialog("Enter a password!"); // Option to input a password
}
}
}
class Login // This is the general windows frame
{
public static void main(String[] a)
{
LoginFrame frame = new LoginFrame();
frame.setTitle("Login Form!");
frame.setVisible(true);
frame.setBounds(600, 100, 360, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
}
}
这就是你的问题:
您将usernameText与自身进行比较,而不是与passwordText相同的值进行比较
首先在类中添加硬编码值:
private static final String username="USER";
private static final String password="pAssw0rd";
然后,将该行更改为:
if (username.equals(usernameText)
&& password.equals(passwordText))
不要在此处使用equalsIgnoreCase
,如果您使用它,也不要将其用作密码。“密码”与“密码”不同,也不应视为相同
如果您这样做,您将看到,现在您的输入已根据这些字段进行验证,任何其他输入都将导致登录无效
编辑:
关于这一部分:
if (myactionevent.getSource() == createAccButton) // This section of code adds functionality to my Register button
{
String usernameText = JOptionPane.showInputDialog("Enter a username!"); // Option to input a username
String passwordText = JOptionPane.showInputDialog("Enter a password!"); // Option to input a password
}
这些是局部变量,只存在于这个if块的范围内,所以不能在这个if块之外使用它们
编辑2:
// if (rulesButton.isSelected()) THIS DOES NOT WORK!
事实上,这很好,但是仅仅因为你点击了一个按钮,并不能使它被选中
既然您已经知道该按钮已被单击,您还需要什么?您如何期望该按钮返回除true以外的任何结果?如果(usernameText.equalsIgnoreCase(usernameText)&&passwordText.equalsIgnoreCase(passwordText)),您的代码确实执行了它应该执行的操作,但没有意义。您正在比较两个字段本身,它们应该通过硬编码值或DB中的值进行比较,或者,更好的是,这些字段的哈希代码应该与DB中的哈希代码进行比较谢谢,我现在明白了,我使用IgnoreCase并将变量=设置为彼此,并期望其他任何东西是多么愚蠢。我忘记了局部变量,也忘记了为什么因为if而不能调用它们block@markinator你能做的就是让JOptionPane设置实例或静态变量,这些是类级别的,所以你可以使用它们;唯一的决赛是permitted@markinator这很可能是因为您将它放在了一个方法中,我希望它位于类级别。在方法中使用private或static确实是不可能的,但在切换到UI编程之前,您应该已经了解了这一点。
// if (rulesButton.isSelected()) THIS DOES NOT WORK!