I am just a beginner in Java. Here, I want to make a User ID & Password input, which will get checked & tell its Right or wrong. But each time it's giving the wrong answer despite inputting the correct combination. Here is my code:
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);
}
}
& This is the CheckID class to check Email & Password:
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";
}
}
}