It is a simple program which allows you to input a username an password. If the username/password is equal to the String it launches a JOptionPane
that says "Correct". If it doesn't it launches a JOptionPane
that says "Incorrect".
import java.awt.event.*;
import javax.swing.*;
public class Main {
//The Strings for the program
static String username = "Username";
static String password = "Password";
static int lockout = 0;
static int lockout1 = 0;
//Main statement, that runs the program
@SuppressWarnings("deprecation")
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
//Makes the program look like regular windows program not a java one
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//The JPanel and it's contents
JPanel p = new JPanel();
JTextField first = new JTextField(8);
first.setToolTipText("Enter Your Username");
JLabel labelUser = new JLabel("Username:");
JPasswordField second = new JPasswordField(8);
//Tooltips when you hover over the item
second.setToolTipText("Enter Your Password");
JLabel labelPass = new JLabel("Password:");
JRadioButton view = new JRadioButton();
view.setToolTipText("To View Your Password");
//Causes the JPassword field to output * as the text.
second.setEchoChar('*');
//Adds the different items to the JPanel.
p.add(labelUser);
p.add(first);
p.add(labelPass);
p.add(second);
p.add(view);
//Adds an action listener to the JRadioButton
ActionListener viewActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(view.isSelected()){
//Makes the Text in the JPasswordField visible.
second.setEchoChar((char)0);
}else{
//Sets it back to the default "**"
second.setEchoChar('*');
}
}
};
//Adds the action listener to the JRadioButton
view.addActionListener(viewActionListener);
//Specifies what buttons are on the JOptionPane
String[] buttons = {"OK", "Cancel"};
//Code for the JOptionPane
int a = JOptionPane.showOptionDialog(null, p, "Authentication", JOptionPane.WARNING_MESSAGE, 1, null, buttons, buttons);
//If the first button is clicked
if (a == 0) {
//Checks if there is any text in the TextFields.
if (first.getText().equals("") || second.getText().equals("")) {
//Adds 1 to the int lockout 1 and if it reaches 4 it closes the app
lockout1 += 1;
if (lockout1 == 4) {
JOptionPane.showMessageDialog(null, "Please try again later", "Try Again Later", 0);
System.exit(0);
}
//Displays the message
JOptionPane.showMessageDialog(null, "Please enter all of your credentials before continuing", "Try Again", 2);
main(args);
//If the username and the password fields are equal to the Strings, it outputs the message correct
} else if (first.getText().equals(username) && second.getText().equals(password)) {
JOptionPane.showMessageDialog(null, "Correct", "Correct", 1);
} else {
//If not it adds 1 to lockout, if lockout reaches 4 the program closes
lockout += 1;
if (lockout == 4) {
JOptionPane.showMessageDialog(null, "Please try again later", "Try Again Later", 0);
System.exit(0);
}
//Outputs the message incorrect
JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", 0);
//reopens the program
main(args);
}
}
//If you click the cancel button, it closes the application
if (a == 1) {
System.exit(0);
}
}
}