I'm making a really basic Login prompt GUI for revision for my Java exam next week (this exercise could possibly come up in the exam so that's why I'm trying to ace it first). Basically when you type in a certain name, in this case 'joe' it'll say verified, otherwise it'll say unverified. However, even when I type in joe it'll still say unverified. Can anyone see where I've gone wrong? Thanks.
package gui.eventHandler;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EventHandlerExample2_NameConfirmation {
private JFrame frame;
private JPanel panel;
private JButton button1;
private JLabel label;
private JTextField fname;
public EventHandlerExample2_NameConfirmation() {
frame = new JFrame("Log In");
panel = new JPanel();
label = new JLabel("First Name: ");
fname = new JTextField(20);
// Actions for when the OK button is clicked
// Implementing the ActionListener as an anonymous inner class
button1 = new JButton("Submit");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputname = fname.getText();
if (inputname == "joe") {
JOptionPane.showMessageDialog(null, "Verified user " + inputname);
} else {
//selects this option regardless of entry
JOptionPane.showMessageDialog(null, "Invalid user " + inputname);
}
}
});
panel.add(label); // add the label
panel.add(fname); // textField
panel.add(button1); // and button to the panel
frame.add(panel); // add panel onto the frame
frame.setVisible(true); // By default its invisible
frame.setSize(600, 70); // 600 width 70 height
frame.setLocation(350, 350); // roughly center screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit when closed
}
public static void main(String[] args) {
EventHandlerExample2_NameConfirmation app = new EventHandlerExample2_NameConfirmation();
}
}