Create a JApplet that asks a user to enter a password into a JTextField and then
ID: 3639889 • Letter: C
Question
Create a JApplet that asks a user to enter a password into a JTextField and then topress Enter. Compare the password to “Rosebud”; if it matches exactly, display “Access
Granted”. If not, display “Access Denied”. Save the file as JPasswordA.java.
Modify the password applet in Exercise 2a to ignore differences in case between the
typed password and “Rosebud”. Save the file as JPasswordB.java.
Modify the password applet in Exercise 2b to compare the password to a list of five
valid passwords: “Rosebud”, “Redrum”, “Jason”, “Surrender”, or “Dorothy”. Save the
file as JPasswordC.java.
Explanation / Answer
Code: /* Create a Swing applet that asks a user to enter a password XXXXX a JTextField and to then press [Enter]. Compare the password XXXXX "Rosebud"; if it matches, display "Access Granted"; if not, display "Access Denied". @author Karyn Choi @version 1.01 */ import javax.swing.*; import java.awt.event.*; import java.awt.*; public class JPasswordA extends JApplet implements ActionListener { Container PW = getContentPane(); JLabel password = new JLabel("Please enter the password"); JTextField input = new JTextField(7); JButton enter = new JButton("Enter"); public void start() { PW.add(password); PW.add(input); PW.add(enter); PW.setLayout(new FlowLayout()); enter.addActionListener(this); } public void actionPerformed(ActionEvent e) { String pass1 = input.getText(); String pass2 = "Rosebud"; if(pass1.equalsIgnoreCase(pass2)) { JOptionPane.showMessageDialog(null, "Access Granted :/)"); } else { JOptionPane.showMessageDialog(null, "Access Denied :/("); } } } You should compile it using javac JPasswordA.java and run using Appletviewer JPassword.java