Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class Password extends JFrame{
- Container cp;
- JPanel panelPassword;
- JPanel panelUser;
- JPanel panelExit;
- JPanel panelHelp, panelLog;
- JPasswordField passWord;
- JPasswordField user;
- JTextField text;
- JTextField textU;
- JButton exitButton, helpButton, logButton;
- JLabel label, labelU;
- int x=3, xP=2;
- public Password (String title)
- {
- setSize(650,150);
- setTitle(title);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //set up the container
- cp = getContentPane();
- user = new JPasswordField(10); //USER PANEL
- //passWord.setActionCommand("OK");
- user.setEchoChar('*');
- user.addActionListener(new ButtonWatcher());
- labelU = new JLabel("Please enter your Username: ");
- labelU.setLabelFor(user);
- textU = new JTextField();
- textU.add(user); //END USER PANEL
- passWord = new JPasswordField(10); //PASSWORD PANEL
- //passWord.setActionCommand("OK");
- passWord.setEchoChar('*');
- passWord.addActionListener(new ButtonWatcher());
- label = new JLabel("Enter the password: ");
- label.setLabelFor(passWord);
- text = new JTextField();
- text.add(passWord); //END PASSWORD PANEL
- //create the buttons
- exitButton = new JButton("Exit"); //EXIT BUTTON
- exitButton.addActionListener(new ButtonWatcher());
- helpButton = new JButton("Help"); //HELP BUTTON
- helpButton.addActionListener(new ButtonWatcher());
- logButton = new JButton("Log In"); //LOGIN IN BUTTON
- logButton.addActionListener(new ButtonWatcher());
- //create the panel and add the passWord field and buttons
- panelPassword = new JPanel();
- panelPassword.setLayout(new FlowLayout());
- panelPassword.add(label);
- panelPassword.add(passWord);
- panelUser = new JPanel();
- panelUser.setLayout(new FlowLayout());
- panelUser.add(labelU);
- panelUser.add(user);
- panelExit = new JPanel();
- panelExit.setLayout(new GridLayout(1,3));
- panelExit.add(exitButton);
- panelExit.add(helpButton);
- panelExit.add(logButton);
- //add the panels to the container
- cp.add(panelPassword,BorderLayout.CENTER);
- cp.add(panelUser,BorderLayout.WEST);
- cp.add(panelExit,BorderLayout.SOUTH);
- }
- //create an inner class to 'listen' to the buttons
- private class ButtonWatcher implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- String cmd = event.getActionCommand();
- Object ob = event.getSource();
- {
- if(ob.equals(logButton) || ob.equals(passWord))
- if( cmd.equalsIgnoreCase("grob") )
- JOptionPane.showMessageDialog(null, "Priority 1, Logout with typing in end as the password");
- else if (cmd.equalsIgnoreCase("muehler") )
- JOptionPane.showMessageDialog(null, "Priority 2, Logout with typing in end as the password");
- else if(cmd.equalsIgnoreCase("alex") )
- JOptionPane.showMessageDialog(null, "Priority 3, Logout with typing in end as the password");
- else if(cmd.equalsIgnoreCase("end"))
- { //END
- JOptionPane.showMessageDialog(null, "System shutting down");
- System.exit(0); } //END
- else { //ERROR
- JOptionPane.showMessageDialog(null,
- "Invalid username or password. "+ xP +" try left",
- "Error Message",
- JOptionPane.ERROR_MESSAGE);
- x=x-1;
- } //ERROR
- if(x==3)
- xP=2;
- if (x==2)
- xP=1;
- if(x==1)
- xP=0;
- if(x==0) {
- JOptionPane.showMessageDialog(null, "You have enter the wrong password three times. The programm will quit");
- System.exit(0);
- }
- }// IF CLASS END
- if(ob.equals(helpButton))
- JOptionPane.showMessageDialog(null, "HELP, Please enter your username and password for your user account.You have "+ x +" tries to enter your password and username");
- {
- if(ob.equals(exitButton))
- {
- setVisible(false);//close the current window
- System.exit(0);//exit the system
- }//end actionPerformed
- }//end ButtonWatcher class
- }
- }
- }
Add Comment
Please, Sign In to add comment