字符串用户&;密码组合不是';不要检查简单的java
我只是Java的初学者。在这里,我想输入一个用户ID和密码,它将被检查并告诉它是对还是错。但每次输入正确的组合,它都会给出错误的答案。 这是我的密码:字符串用户&;密码组合不是';不要检查简单的java,java,if-statement,passwords,Java,If Statement,Passwords,我只是Java的初学者。在这里,我想输入一个用户ID和密码,它将被检查并告诉它是对还是错。但每次输入正确的组合,它都会给出错误的答案。 这是我的密码: import java.awt.Container; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JPasswordField
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Zihan {
public JFrame frame;
public JTextField textField;
public JPasswordField passwordField;
public static String gtxt,ptxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Zihan window = new Zihan();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Zihan() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblEmailuserId = new JLabel("Email/User ID:");
lblEmailuserId.setBounds(76, 30, 90, 14);
frame.getContentPane().add(lblEmailuserId);
textField = new JTextField();
textField.setBounds(171, 27, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(171, 87, 86, 20);
frame.getContentPane().add(passwordField);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(76, 90, 69, 14);
frame.getContentPane().add(lblPassword);
JButton btnLogIn = new JButton("Log In");
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
gtxt = textField.getText();
ptxt = passwordField.getText();
new CheckID(gtxt,ptxt);
JFrame login = new JFrame("Login window");
login.setSize(200, 200);
login.setVisible(true);
JLabel msg = new JLabel();
msg.setText(CheckID.msgt);
login.getContentPane().add(msg);
frame.setVisible(false);
}
});
btnLogIn.setBounds(121, 137, 89, 23);
frame.getContentPane().add(btnLogIn);
}
}
&这是用于检查电子邮件和密码的CheckID类:
public class CheckID {
public static String msgt;
String cU = "zihan";
String cP = "123";
CheckID(String tx ,String px){
if(tx==cU && px ==cP)
{
msgt = "Welcome to messenger";
}
else
{
msgt = "Sorry! Wrong Password/Email";
}
}
}改变
if(tx==cU && px ==cP)
到
=
不是Java中比较字符串的方式,您需要使用String#equals
if(tx.equals(cU) && px.equals(cP))