|
| 1 | +/** |
| 2 | + * Programmer: Sean Thames |
| 3 | + * Date: 2013-07-25 |
| 4 | + * Filename: JPasswordA.java |
| 5 | + * Assignment: Ch 17 Ex 2 |
| 6 | + * |
| 7 | + * Description: A) Create a JApplet that asks a user to enter a |
| 8 | + * password into a JTextField and to then press Enter. Compare the |
| 9 | + * password to "Rosebud"; if it matches exactly, display "Access Granted". |
| 10 | + * If not, display "Access Denied". Save the file as JPasswordA.java. |
| 11 | + * |
| 12 | + * B) Modify the password applet in Exercise 2a to ignore differences in |
| 13 | + * case between the typed password and "Rosebud". Save the file as |
| 14 | + * JPasswordB.java. |
| 15 | + * |
| 16 | + * C) Modify the password applet in Exercise 2b to compare the password |
| 17 | + * to a list of five valid passwords: Rosebud, Redrum, Jason, Surrender, |
| 18 | + * or Dorothy. Save the file as JPasswordC.java |
| 19 | + */ |
| 20 | + |
| 21 | +import javax.swing.*; |
| 22 | +import java.awt.*; |
| 23 | +import java.awt.event.*; |
| 24 | + |
| 25 | +public class JPasswordC extends JApplet implements ActionListener |
| 26 | +{ |
| 27 | + private final String[] PASSWORD = {"Rosebud", "Redrum", "Jason", "Surrender", "Dorothy"}; |
| 28 | + |
| 29 | + private Container con = getContentPane(); |
| 30 | + private JLabel passwordLabel = new JLabel("Password:"); |
| 31 | + private JTextField passwordField = new JTextField(16); |
| 32 | + |
| 33 | + private JLabel accessGranted = new JLabel("<html><font color=\"green\">Access Granted</font></html>"); |
| 34 | + private JLabel accessDenied = new JLabel("<html><font color=\"red\">Access Denied</font></html>"); |
| 35 | + |
| 36 | + public void init() |
| 37 | + { |
| 38 | + con.setLayout(new FlowLayout()); |
| 39 | + |
| 40 | + con.add(passwordLabel); |
| 41 | + con.add(passwordField); |
| 42 | + |
| 43 | + con.add(accessGranted); |
| 44 | + accessGranted.setVisible(false); |
| 45 | + con.add(accessDenied); |
| 46 | + accessDenied.setVisible(false); |
| 47 | + |
| 48 | + passwordField.addActionListener(this); |
| 49 | + } |
| 50 | + |
| 51 | + public void actionPerformed(ActionEvent ae) |
| 52 | + { |
| 53 | + String input = passwordField.getText(); |
| 54 | + |
| 55 | + for(String p : PASSWORD) |
| 56 | + { |
| 57 | + if(input.equalsIgnoreCase(p)) |
| 58 | + { |
| 59 | + accessGranted.setVisible(true); |
| 60 | + accessDenied.setVisible(false); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + accessDenied.setVisible(true); |
| 65 | + accessGranted.setVisible(false); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments