Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package student_manager;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.BoxLayout;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import javax.swing.SwingConstants;
- import javax.swing.JPasswordField;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.util.Arrays;
- import java.awt.event.ActionEvent;
- import javax.swing.JSeparator;
- import javax.swing.UIManager;
- public class LoginWindow extends JFrame implements ActionListener{
- public static LoginWindow loginFrame = new LoginWindow();
- private JPanel contentPane;
- private JTextField tfUsername;
- private JPasswordField passwordField;
- private JButton btnLogin;
- private JButton btnCancel;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- try
- {
- loginFrame.setVisible(true);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public LoginWindow() {
- setTitle("Student Manager - Log in");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBackground(new Color(51, 153, 204));
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel lblWelcome = new JLabel("Welcome to Student Manager,");
- lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
- lblWelcome.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 23));
- lblWelcome.setBounds(59, 11, 328, 23);
- contentPane.add(lblWelcome);
- JLabel lblUsername = new JLabel("Username");
- lblUsername.setForeground(new Color(51, 51, 51));
- lblUsername.setFont(new Font("Arial", Font.BOLD, 18));
- lblUsername.setBounds(86, 88, 89, 17);
- contentPane.add(lblUsername);
- JLabel lblPassword = new JLabel("Password");
- lblPassword.setForeground(new Color(51, 51, 51));
- lblPassword.setFont(new Font("Arial", Font.BOLD, 18));
- lblPassword.setBounds(86, 132, 89, 17);
- contentPane.add(lblPassword);
- JLabel lblNewLabel_1 = new JLabel("Please log in to continue!");
- lblNewLabel_1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 23));
- lblNewLabel_1.setBounds(86, 35, 279, 26);
- contentPane.add(lblNewLabel_1);
- tfUsername = new JTextField();
- tfUsername.setFont(UIManager.getFont("TextField.font"));
- tfUsername.setBounds(185, 85, 120, 23);
- contentPane.add(tfUsername);
- tfUsername.setColumns(10);
- passwordField = new JPasswordField();
- passwordField.setFont(UIManager.getFont("PasswordField.font"));
- passwordField.setBounds(185, 129, 120, 23);
- contentPane.add(passwordField);
- btnLogin = new JButton("LOG IN");
- btnLogin.setBackground(new Color(204, 204, 204));
- btnLogin.setFont(new Font("Arial", Font.BOLD, 14));
- btnLogin.setBounds(86, 195, 89, 36);
- contentPane.add(btnLogin);
- btnLogin.addActionListener(this);
- btnCancel = new JButton("CANCEL");
- btnCancel.setBackground(new Color(204, 204, 204));
- btnCancel.setFont(new Font("Arial", Font.BOLD, 14));
- btnCancel.setBounds(276, 195, 89, 36);
- contentPane.add(btnCancel);
- btnCancel.addActionListener(this);
- JSeparator separator1 = new JSeparator();
- separator1.setForeground(new Color(0, 51, 102));
- separator1.setToolTipText("");
- separator1.setBackground(new Color(0, 51, 102));
- separator1.setBounds(10, 67, 414, 9);
- contentPane.add(separator1);
- JSeparator separator2 = new JSeparator();
- separator2.setForeground(new Color(0, 51, 102));
- separator2.setBackground(new Color(0, 51, 102));
- separator2.setBounds(10, 174, 414, 9);
- contentPane.add(separator2);
- }
- private static boolean isPasswordCorrect(char[] input) {
- boolean isCorrect = true;
- char[] correctPassword = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };
- if (input.length != correctPassword.length) {
- isCorrect = false;
- } else {
- isCorrect = Arrays.equals (input, correctPassword);
- }
- //Zero out the password.
- Arrays.fill(correctPassword,'0');
- return isCorrect;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- char[] input = passwordField.getPassword();
- if (e.getSource() == btnLogin){
- if (tfUsername.getText().equals("Admin") && isPasswordCorrect(input)){
- loginFrame.setVisible( false );
- CreateRecords.createFrame.setVisible(true);
- }
- else {
- JOptionPane.showMessageDialog(null, "Invalid username or password!");
- }
- }
- if (e.getSource() == btnCancel){
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement