Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- public class loginsource extends JFrame
- {
- // Variables declaration
- private JLabel jLabel1;
- private JLabel jLabel2;
- private JTextField jTextField1;
- private JPasswordField jPasswordField1;
- private JButton jButton1;
- private JPanel contentPane;
- // End of variables declaration
- public loginsource()
- {
- super();
- create();
- this.setVisible(true);
- }
- private void create()
- {
- jLabel1 = new JLabel();
- jLabel2 = new JLabel();
- jTextField1 = new JTextField();
- jPasswordField1 = new JPasswordField();
- jButton1 = new JButton();
- contentPane = (JPanel)this.getContentPane();
- //
- // jLabel1
- //
- jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
- jLabel1.setText("Username:");
- //
- // jLabel2
- //
- jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
- jLabel2.setText("Password:");
- //
- // jTextField1
- //
- jTextField1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- jTextField1_actionPerformed(e);
- }
- });
- //
- // jPasswordField1
- //
- jPasswordField1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- jPasswordField1_actionPerformed(e);
- }
- });
- //
- // jButton1
- //
- jButton1.setBackground(new Color(204, 204, 204));
- jButton1.setText("Login");
- jButton1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- jButton1_actionPerformed(e);
- }
- });
- //
- // contentPane
- //
- contentPane.setLayout(null);
- contentPane.setBorder(BorderFactory.createEtchedBorder());
- contentPane.setBackground(new Color(204, 204, 204));
- addComponent(contentPane, jLabel1, 5,10,106,18);
- addComponent(contentPane, jLabel2, 5,47,97,18);
- addComponent(contentPane, jTextField1, 110,10,183,22);
- addComponent(contentPane, jPasswordField1, 110,45,183,22);
- addComponent(contentPane, jButton1, 135,75,83,28);
- //
- // login
- //
- this.setTitle("fail0verflow Login Window");
- this.setLocation(new Point(76, 182));
- this.setSize(new Dimension(335, 141));
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- this.setResizable(false);
- }
- /** Add Component Without a Layout Manager (Absolute Positioning) */
- private void addComponent(Container container,Component c,int x,int y,int width,int height)
- {
- c.setBounds(x,y,width,height);
- container.add(c);
- }
- private void jTextField1_actionPerformed(ActionEvent e)
- {
- }
- private void jPasswordField1_actionPerformed(ActionEvent e)
- {
- }
- private void jButton2_actionPerformed(ActionEvent e)
- {
- }
- private void jButton1_actionPerformed(ActionEvent e)
- {
- String username = new String(jTextField1.getText());
- String password = new String(jPasswordField1.getPassword());
- if(username.equals("") || password.equals("")) // If password and username is empty > Do this >>>
- {
- jButton1.setEnabled(false);
- JOptionPane.showMessageDialog(null,"You must enter a username and password to login.");
- jTextField1.setText("");
- jPasswordField1.setText("");
- jButton1.setEnabled(true);
- this.setVisible(true);
- }
- else
- {
- int confirm =JOptionPane.showConfirmDialog(null,"<HTML>You entered<FONT COLOR = RED> <B>"+username+
- "</B></FONT> as your username.<BR> Do you want to continue?</HTML>");
- switch(confirm){ // Switch > Case
- case JOptionPane.YES_OPTION: // Attempt to Login user
- jButton1.setEnabled(false); // Set button enable to false to prevent 2 login attempts
- File file = new File("C:/text.txt");
- try{
- boolean u = false;
- boolean p = false;
- char[] pass = jPasswordField1.getPassword();
- String f = "";
- String c = new String(pass);
- Scanner s = new Scanner(file);
- while(s.hasNextLine()) {
- String[] a = s.nextLine().split(",");
- int x = 0;
- for(; x < a.length; x+=2)
- {
- if(username.equals(a[x]))
- {
- u = true;
- f = a[x];
- break;
- }
- else{continue;}
- }
- for(; x < a.length; x+=1)
- {
- if(c.equals(a[x]))
- {
- p = true;
- }
- }
- }
- if(u == true && p == true) {
- JOptionPane.showMessageDialog(null, "Login success.\nWelcome " + f +"!");
- jButton1.setEnabled(true);
- } else {
- JOptionPane.showMessageDialog(null, "Invalid Account!");
- jButton1.setEnabled(true);
- }
- }
- catch (Exception db)
- {
- JOptionPane.showMessageDialog(null, "Error reading database.\nProgram will now exit.", "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(0);
- }
- this.setVisible(true);
- break;
- case JOptionPane.NO_OPTION: // No Case.(Go back. Set text to 0)
- jButton1.setEnabled(false);
- jTextField1.setText("");
- jPasswordField1.setText("");
- jButton1.setEnabled(true);
- break;
- case JOptionPane.CANCEL_OPTION: // Cancel Case.(Go back. Set text to 0)
- jButton1.setEnabled(false);
- jTextField1.setText("");
- jPasswordField1.setText("");
- jButton1.setEnabled(true);
- break;
- } // End Switch > Case
- }
- }
- public static void main(String[] args)
- {
- JFrame.setDefaultLookAndFeelDecorated(true);
- JDialog.setDefaultLookAndFeelDecorated(true);
- try
- {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- }
- catch (Exception ex)
- {
- System.out.println("Failed loading L&F: ");
- System.out.println(ex);
- }
- new loginsource();
- };
- }
Add Comment
Please, Sign In to add comment