|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Arrays; public class PasswordFieldExample implements ActionListener{ JPanel textPanel, panelForTextFields, completionPanel; JLabel titleLabel, usernameLabel, passwordLabel, userLabel, passLabel; JTextField usernameField; JPasswordField loginField; JButton loginButton; public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. JPanel totalGUI = new JPanel(); totalGUI.setLayout(null); titleLabel = new JLabel("Login Screen"); titleLabel.setLocation(0,0); titleLabel.setSize(290, 30); titleLabel.setHorizontalAlignment(0); totalGUI.add(titleLabel); // Creation of a Panel to contain the JLabels textPanel = new JPanel(); textPanel.setLayout(null); textPanel.setLocation(10, 35); textPanel.setSize(70, 80); totalGUI.add(textPanel); // Username Label usernameLabel = new JLabel("Username"); usernameLabel.setLocation(0, 0); usernameLabel.setSize(70, 40); usernameLabel.setHorizontalAlignment(4); textPanel.add(usernameLabel); // Login Label passwordLabel = new JLabel("Password"); passwordLabel.setLocation(0, 40); passwordLabel.setSize(70, 40); passwordLabel.setHorizontalAlignment(4); textPanel.add(passwordLabel); // TextFields Panel Container panelForTextFields = new JPanel(); panelForTextFields.setLayout(null); panelForTextFields.setLocation(110, 40); panelForTextFields.setSize(100, 70); totalGUI.add(panelForTextFields); // Username Textfield usernameField = new JTextField(8); usernameField.setLocation(0, 0); usernameField.setSize(100, 30); panelForTextFields.add(usernameField); // Login Textfield loginField = new JPasswordField(8); loginField.setEchoChar('&'); loginField.setLocation(0, 40); loginField.setSize(100, 30); panelForTextFields.add(loginField); // Creation of a Panel to contain the completion JLabels completionPanel = new JPanel(); completionPanel.setLayout(null); completionPanel.setLocation(240, 35); completionPanel.setSize(70, 80); totalGUI.add(completionPanel); // Username Label userLabel = new JLabel("Wrong"); userLabel.setForeground(Color.red); userLabel.setLocation(0, 0); userLabel.setSize(70, 40); completionPanel.add(userLabel); // Login Label passLabel = new JLabel("Wrong"); passLabel.setForeground(Color.red); passLabel.setLocation(0, 40); passLabel.setSize(70, 40); completionPanel.add(passLabel); // Button for Logging in loginButton = new JButton("Login"); loginButton.setLocation(130, 120); loginButton.setSize(80, 30); loginButton.addActionListener(this); totalGUI.add(loginButton); totalGUI.setOpaque(true); return totalGUI; } // With this action performed, we simply check to see if the username and // password match "Bob" as the username and "Robert" as the password. // If they do, we set the labels ajacent to them to "Correct!" and color // them green. // At the end, we check if both labels are green. If they are, we set the // screen to be 'Logging In'. public void actionPerformed(ActionEvent e) { if(e.getSource() == loginButton) { if(usernameField.getText().trim().compareTo("Bob") == 0) { userLabel.setForeground(Color.green); userLabel.setText("Correct!"); } else { userLabel.setForeground(Color.red); userLabel.setText("Wrong!"); } // Here, because we use a password field, we use the getPassword // command. This is more secure. // Once we are finished with the char array with the correct answer // we change it back to blank. // You may ask why we do this when the char array we compare it to // is in clear text one line above. // Obviously we'd store this in an encrypted database. (or something // along those lines!) char[] answer = {'R', 'o', 'b', 'e', 'r', 't'}; char[] input = loginField.getPassword(); if(Arrays.equals(input, answer)) { passLabel.setForeground(Color.green); passLabel.setText("Correct!"); for(int i = 0; i < input.length; i++) { input[i] = ' '; } } else { passLabel.setForeground(Color.red); passLabel.setText("Wrong!"); } if((userLabel.getForeground() == Color.green) && (passLabel.getForeground() == Color.green)) { titleLabel.setText("Logging in...."); loginButton.setEnabled(false); } } } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] JPassword of Secrets! [=]"); PasswordFieldExample demo = new PasswordFieldExample(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(310, 200); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } // ************************************************************* // Name : PasswordFieldExample.java // Author : Stuart Davidson // Date : 21/12/2006 // Description : Shows how a PasswordField is different to a TextField. // ************************************************************* |