I am trying to make a program in its own JFrame that has a JPasswordField that you type in something and it checks to see if its the password. Also there are two hint buttons next to the password field that gives you hints. When i ran my program everything worked fine except when i actually typed in the right password it wouldn't work! I have no idea what i did wrong and this is what I typed(not including the main method because that is in another class):
package PasswordGame;
< import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JFrame; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Crap extends JFrame { private JPasswordField Pass; private JLabel Label; private JButton Hint1; private JButton Hint2; public Crap() { super("Hello"); setLayout( new FlowLayout()); Label= new JLabel("Guess the password or click the button for a hint!"); add(Label); Pass = new JPasswordField(" "); add(Pass); Hint1 = new JButton("Hint 1"); Hint2 = new JButton("Hint 2"); add(Hint1); add(Hint2); thehandler handler = new thehandler(); Hint1.addActionListener(handler); Pass.addActionListener(handler); Hint2.addActionListener(handler); } private class thehandler implements ActionListener { public void actionPerformed(ActionEvent event) { String string= ""; String abc= "abc"; String ABC= "ABC"; if(event.getSource()==Pass) { string=String.format(event.getActionCommand()); if(string==abc) { JOptionPane.showMessageDialog(null, "YOU GOT IT RIGHT! YAY!"); } else if(string==ABC) { JOptionPane.showMessageDialog(null, "YOU GOT IT RIGHT! YAY!"); } else { JOptionPane.showMessageDialog(null, "Your wrong, try again! :P"); } } else if(event.getSource()==Hint1) { JOptionPane.showMessageDialog(null, "Hint 1: It is three letters..."); } else if(event.getSource()==Hint2) { JOptionPane.showMessageDialog(null, "Hint 2: Michael Jackson sings this in a song..."); } } } }>
So I have no idea why when it checks if string is equal to abc it screws up so if someone could please help me it would be much appreciated! Then again thanks if you actually bothered to read and try to solve it. If you need to ask me any questions just comment. (or there is a typo!)