can I ask how I can instantly equal my TextField named txtUserName into 'Aime' and also my txtPassword equal to 'Joy' so that it will show the message "User Name and Password Match!" ? Anyone? Please help me :(
public void actionPerformed(ActionEvent e){
if (e.getSource()== btnClear){
txtUserName.setText("");
}
if(e.getSource() == btnLogin){
if (txtUserName.setText="Aime" && txtPassword.setText="JOy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}
else {
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
These are the Small modifications needed.
if (txtUserName.getText().equals("Aime") && txtPassword.getText().equals("Joy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}else{
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
For your reference : What is the difference between == vs equals() in Java?
you have two problems here:
txtUserName.setText="Aime" && txtPassword.setText="JOy"
am guessing you are trying to check the user input so you need to use the gettext method...
on the other hand you can not compare strings using ==, but furthermore you mistyped the == and are instead of comparing assigning, assignment that is invalid since you can not set the text of that view in that way...
try instead
txtUserName.getText().toString().equals("Aime") && ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With