My assignment is to create a password validation applet, that compares user input to a specified set of passwords stored in an array.
For some reason, no matter what I type in, it gives me the error saying the password is incorrect. I'm not sure why this is, any help would be much appreciated.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Password extends JApplet implements ActionListener { protected String pass; protected Container con = getContentPane(); protected Font fntHeading = new Font("Times Roman", Font.ITALIC, 20); protected JLabel labelGreeting = new JLabel("Enter Password (and click OK):"); protected JTextField txtPassword = new JTextField("", 10); protected JButton btnOk = new JButton("OK"); protected JTextField txtAccess = new JTextField("Access Granted"); protected JTextField txtDenied = new JTextField ("Access Denied; Please re-enter a valid password"); String [] Password = {"rosebud", "redrum", "jason", "surrender", "dorothy"}; public void init() { FlowLayout layout = new FlowLayout(); con.setLayout(layout); labelGreeting.setFont(fntHeading); con.add(labelGreeting); con.add(txtPassword); con.add(btnOk); btnOk.addActionListener(this); } public void actionPerformed(ActionEvent authenticate) { pass = txtPassword.getText(); if (pass == Password[0] || pass == Password[1] || pass == Password[2] || pass == Password[3] || pass == Password[4]) con.add(txtAccess); else con.add(txtDenied); con.validate(); } }
Also, from the instructions, the user is supposed to also be able to press enter in order to access the actionevent..... however I couldn't really find anywhere that specified how to do such a thing, and nothing happens when I press enter, I assume it's because I haven't created an eventlistener for it. Thank you.