A) Create a JApplet that asks a user to enter a password into a JTextField and t
ID: 672751 • Letter: A
Question
A) Create a JApplet that asks a user to enter a password into a JTextField and to then press Enter. Compare the password to Rosebud; if it matches exactly, display "Access Granted". If not, display "Access Denied". Save the file as JPasswordA.java
B) Modify the password applet in Exercise A to ignore difference in case between the typed password and Rosebud. Save the file as JPasswordB.java.
C) 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
/*
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 :/(");
}
}
}