英文:
Trying to make a login page, I am stuck! [java]
问题
这是我的代码,我想要做的是,当点击登录按钮时,希望它检查密码和文本字段中的密码是否正确(在if语句中找到)。我需要帮助获取文本字段和密码字段中的输入。当我运行代码时,它直接跳转到else语句并执行其中的代码,我希望它执行if语句,因为我已经输入了正确的用户名和密码。我试图弄清楚如何从文本字段获取输入,但我不知道该怎么做。我会感激一些帮助,谢谢。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class GUI_2 implements ActionListener {
private JLabel PassLabel;
private JFrame frame;
private JButton enterButton;
private JLabel UserLabel;
private JLabel label;
private JPanel panel;
private JFrame Incorrect;
private JTextField password;
private JTextField username;
private String rightPassword;
private String rightUsername;
private String passwordInput;
private String usernameInput;
private JButton UsernameEnter;
private JButton PasswordEnter;
private final static String newline = "\n";
private TextArea textArea;
public GUI_2() {
PassLabel = new JLabel("Enter Password:");
password = new JPasswordField(11);
UserLabel = new JLabel("Enter Username:");
username = new JTextField(11);
enterButton = new JButton("Login");
label = new JLabel("Access");
UsernameEnter = new JButton("Enter");
PasswordEnter = new JButton("Enter");
frame = new JFrame();
panel = new JPanel();
Incorrect = new JFrame();
enterButton.addActionListener(this);
panel.setBorder(BorderFactory.createEmptyBorder(200, 200, 60, 300));
panel.setLayout(new GridLayout(5, 5));
panel.add(UserLabel);
panel.add(username);
panel.add(PassLabel);
panel.add(password);
panel.add(label);
panel.add(enterButton);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Password Login");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI_2();
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent arg0) {
// 获取文本字段中的输入
usernameInput = username.getText();
passwordInput = new String(password.getPassword());
if (passwordInput.equals("password") && usernameInput.equals("harry")) {
frame.setTitle("Success");
label.setForeground(Color.green);
label.setText("Access granted");
} else {
frame.setTitle("Access Denied");
label.setForeground(Color.red);
label.setText("Access Denied");
}
}
}
我已经将代码中获取文本字段和密码字段输入的部分进行了更新。
英文:
Here is my code and what I want to do is, when the login button is pressed, I would like it to check the password and text fields for the right password(found in the if statement). I need help on what to do to get the input from the text and password fields. When I run the code, it skips right to the else statement and does the code in there, I want it to do the if statement as I have entered the right username and password. I was trying to figure out how to get input from the text fields but I don't know how to. I would appreciate some help, thank you.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class GUI_2 implements ActionListener {
private JLabel PassLabel;
private JFrame frame;
private JButton enterButton;
private JLabel UserLabel;
private JLabel label;
private JPanel panel;
private JFrame Incorrect;
private JTextField password;
private JTextField username;
private String rightPassword;
private String rightUsername;
private String passwordInput;
private String usernameInput;
private JButton UsernameEnter;
private JButton PasswordEnter;
private final static String newline = "\n";
private TextArea textArea;
public GUI_2()
{
PassLabel = new JLabel("Enter Password:");
password = new JPasswordField(11);
UserLabel = new JLabel("Enter Username:");
username = new JTextField(11);
enterButton = new JButton("Login");
label = new JLabel("Access");
UsernameEnter = new JButton("Enter");
PasswordEnter = new JButton("Enter");
frame = new JFrame();
panel = new JPanel();
Incorrect = new JFrame();
enterButton.addActionListener(this);
panel.setBorder(BorderFactory.createEmptyBorder(200,200,60,300));
panel.setLayout(new GridLayout(5, 5));
panel.add(UserLabel);
panel.add(username);
panel.add(PassLabel);
panel.add(password);
panel.add(label);
panel.add(enterButton);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Password Login");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI_2();
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent arg0)
{
if (passwordInput == "password" && usernameInput == "harry")
{
frame.setTitle("Success");
label.setForeground(Color.green);
label.setText("Access granted");
}
else
{
frame.setTitle("Access Denied");
label.setForeground(Color.red);
label.setText("Access Denied");
}
}
}
答案1
得分: 1
1: 你可以通过以下方式获取输入密码:password.getText()
2: 你应该使用 "equals" 来比较两个字符串 passwordInput.equals("password")
所以修改 actionPerformed 方法如下:
public void actionPerformed(ActionEvent arg0) {
passwordInput = password.getText();
usernameInput = username.getText();
if (passwordInput != null
&& usernameInput != null
&& passwordInput.equals("password")
&& usernameInput.equals("harry")) {
frame.setTitle("Success");
label.setForeground(Color.green);
label.setText("Access granted");
} else {
frame.setTitle("Access Denied");
label.setForeground(Color.red);
label.setText("Access Denied");
}
}
英文:
1: you can get the input password with: password.getText()
2: you should use "equals" compare two string passwordInput.equals("password")
so modify the actionPerformed method like this:
public void actionPerformed(ActionEvent arg0) {
passwordInput = password.getText();
usernameInput = username.getText();
if (passwordInput != null
&& usernameInput != null
&& passwordInput.equals("password")
&& usernameInput.equals("harry")) {
frame.setTitle("Success");
label.setForeground(Color.green);
label.setText("Access granted");
} else {
frame.setTitle("Access Denied");
label.setForeground(Color.red);
label.setText("Access Denied");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论