Advertisement
notjacob

login java but its in a window and its a program

Jan 16th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.29 KB | None | 0 0
  1. package com.notjacob.main;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10.  
  11. import javax.imageio.ImageIO;
  12. import javax.swing.GroupLayout;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JTextArea;
  18.  
  19.  
  20. public class Main {
  21.    
  22.     public static windowstate currentstate;
  23.     public static windowstate laststate;
  24.     public static ArrayList<user> usermap = new ArrayList<user>();
  25.     public static boolean getPasswordMatches(user uzer, String password) {
  26.         return uzer.getPassword().equalsIgnoreCase(password) ? true : false;
  27.     }
  28.     public static boolean getUserExists(String user) {
  29.         for (int i = 0; i < usermap.size(); i++) {
  30.             if (usermap.get(i).getUsername().equalsIgnoreCase(user)) {
  31.                 return true;
  32.             }
  33.         }
  34.         return false;
  35.     }
  36.     public static user userFromString(String user) {
  37.         for (int i = 0; i < usermap.size(); i++) {
  38.             if (usermap.get(i).getUsername().equalsIgnoreCase(user)) {
  39.                 return usermap.get(i);
  40.             }
  41.         }
  42.         return null;
  43.     }
  44.    
  45.    
  46.     public static void main(String[] args) {
  47.         new user("username", "password");
  48.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  49.             public void run() {
  50.                 initFrame();
  51.                 optionsScreen();
  52.             }
  53.         });
  54.     }
  55.    
  56.     static JFrame frame = new JFrame();
  57.     static PanelBG ControlPanel = new PanelBG();
  58.     static JButton clickButton = new JButton("Confirm");
  59.     static JLabel isvalid = new JLabel("Button not clicked");
  60.     static JTextArea username = new JTextArea();
  61.     static JTextArea password = new JTextArea();
  62.     static JLabel success = new JLabel("Login success!");
  63.     static JButton logout = new JButton("logout");
  64.     static JButton loginButton = new JButton("Login");
  65.     static JButton registerButton = new JButton("Register");
  66.     static JButton clickBu2ton = new JButton("Confirm");
  67.     static JLabel isvalidr = new JLabel("Button not clicked");
  68.     static JTextArea rusername = new JTextArea();
  69.     static JTextArea rpassword = new JTextArea();
  70.     static JButton back = new JButton("back");
  71.     static JLabel configs = new JLabel("Success!");
  72.     public static void initFrame() {
  73.         laststate = windowstate.OPTIONS;
  74.         frame.setTitle("login window");
  75.         frame.setLayout(new BorderLayout());
  76.         frame.add(ControlPanel, BorderLayout.CENTER);
  77.         frame.setSize(new Dimension(800, 650));
  78.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79.         frame.setLocationRelativeTo(null);
  80.         frame.setResizable(false);
  81.         ControlPanel.setLayout(new GroupLayout(ControlPanel));
  82.         ControlPanel.add(back);
  83.         back.setBounds(750, 600, 200, 40);
  84.         frame.setVisible(true);
  85.         back.addActionListener((e) -> {
  86.             executeFor(laststate);
  87.             frame.revalidate();
  88.             frame.repaint();
  89.         });
  90.        
  91.     }
  92.     public static void loingScreen() {
  93.         isvalid.setText("Button not clicked");
  94.         isvalidr.setText("Button not clicked");
  95.         currentstate = windowstate.LOGIN;
  96.         ControlPanel.removeAll();
  97.         ControlPanel.add(back);
  98.         ControlPanel.add(isvalid);
  99.         ControlPanel.add(username);
  100.         ControlPanel.add(password);
  101.         ControlPanel.add(clickButton);
  102.         username.setBounds(100, 100, 200, 40);
  103.         password.setBounds(100, 200, 200, 40);
  104.         isvalid.setBounds(100, 340, 400, 40);
  105.         clickButton.setBounds(100, 200, 340, 40);
  106.         frame.revalidate();
  107.         frame.repaint();
  108.         clickButton.addActionListener((e) -> {
  109.             if (!username.getText().isEmpty()) {
  110.                 if (getUserExists(username.getText())) {
  111.                     if (!password.getText().isEmpty()) {
  112.                         if (getPasswordMatches(userFromString(username.getText()), password.getText())) {
  113.                             laststate = windowstate.LOGIN;
  114.                             login();
  115.                         } else {
  116.                             isvalid.setText("invalid password!");
  117.                         }
  118.                     } else {
  119.                         isvalid.setText("add a password!");
  120.                     }
  121.                 } else {
  122.                     isvalid.setText("user is not valid!");
  123.                 }
  124.             } else {
  125.                 isvalid.setText("need a user!");
  126.             }
  127.         });
  128.     }
  129.     public static void optionsScreen() {
  130.         isvalid.setText("Button not clicked");
  131.         isvalidr.setText("Button not clicked");
  132.         currentstate = windowstate.OPTIONS;
  133.         ControlPanel.removeAll();
  134.         ControlPanel.add(back);
  135.         ControlPanel.add(loginButton);
  136.         ControlPanel.add(registerButton);
  137.         loginButton.setBounds(300, 300, 300, 40);
  138.         registerButton.setBounds(300, 400, 300, 40);
  139.         frame.revalidate();
  140.         frame.repaint();
  141.         loginButton.addActionListener((e) -> {
  142.             ControlPanel.remove(loginButton);
  143.             ControlPanel.remove(registerButton);
  144.             ControlPanel.remove(isvalidr);
  145.             ControlPanel.remove(isvalid);
  146.             loingScreen();
  147.         });
  148.         registerButton.addActionListener((e) -> {
  149.             ControlPanel.remove(loginButton);
  150.             ControlPanel.remove(registerButton);
  151.             ControlPanel.remove(isvalidr);
  152.             ControlPanel.remove(isvalid);
  153.             laststate = windowstate.OPTIONS;
  154.             doRegisterScreen();
  155.         });
  156.     }
  157.     public static void doRegisterScreen() {
  158.         isvalid.setText("Button not clicked");
  159.         isvalidr.setText("Button not clicked");
  160.         currentstate = windowstate.REGISTER;
  161.         ControlPanel.removeAll();
  162.         ControlPanel.add(back);
  163.         ControlPanel.add(rpassword);
  164.         ControlPanel.add(rusername);
  165.         ControlPanel.add(isvalidr);
  166.         ControlPanel.add(clickBu2ton);
  167.         rpassword.setBounds(100, 200, 200, 40);
  168.         rusername.setBounds(100, 100, 200, 40);
  169.         isvalidr.setBounds(100, 340, 400, 40);
  170.         clickBu2ton.setBounds(100, 200, 340, 40);
  171.         frame.revalidate();
  172.         frame.repaint();
  173.         clickBu2ton.addActionListener((e) -> {
  174.             if (!rusername.getText().isEmpty()) {
  175.                 if (!getUserExists(rusername.getText())) {
  176.                     if (!rpassword.getText().isEmpty()) {
  177.                         new user(rusername.getText(), rpassword.getText());
  178.                         success.setText("Registered!");
  179.                         success.setBounds(40, 40, 100, 40);
  180.                         ControlPanel.add(success);
  181.                         ControlPanel.remove(rpassword);
  182.                         ControlPanel.remove(rusername);
  183.                         ControlPanel.remove(clickBu2ton);
  184.                         laststate = windowstate.REGISTER;
  185.                         optionsScreen();
  186.                     } else {
  187.                         isvalidr.setText("Need a password!");
  188.                     }
  189.                 } else {
  190.                     isvalidr.setText("That user already exists!");
  191.                 }
  192.             } else {
  193.                 isvalidr.setText("Need a username!");
  194.             }
  195.         });
  196.     }
  197.     public static void login() {
  198.         isvalid.setText("Button not clicked");
  199.         isvalidr.setText("Button not clicked");
  200.         currentstate = windowstate.LOGGEDIN;
  201.         ControlPanel.removeAll();
  202.         ControlPanel.add(back);
  203.         username.setText("");
  204.         password.setText("");
  205.         ControlPanel.remove(username);
  206.         ControlPanel.remove(clickButton);
  207.         ControlPanel.remove(isvalid);
  208.         ControlPanel.remove(password);
  209.         success.setText("Logged in!");
  210.         ControlPanel.add(success);
  211.         ControlPanel.add(logout);
  212.         success.setBounds(300, 300, 300, 40);
  213.         logout.setBounds(300, 380, 300, 40);
  214.         frame.revalidate();
  215.         frame.repaint();
  216.         logout.addActionListener((e) -> {
  217.             laststate = windowstate.OPTIONS;
  218.             logout();
  219.         });
  220.     }
  221.     public static void logout() {
  222.        
  223.         ControlPanel.remove(success);
  224.         ControlPanel.remove(logout);
  225.         success.setText("Logged out!");
  226.         success.setBounds(40, 40, 100, 40);
  227.         ControlPanel.add(success);
  228.         laststate = windowstate.OPTIONS;
  229.         optionsScreen();
  230.     }
  231.     enum windowstate {
  232.         OPTIONS,
  233.         SELECT,
  234.         LOGIN,
  235.         REGISTER,
  236.         LOGGEDIN;
  237.        
  238.     }
  239.     public static void executeFor(windowstate state) {
  240.         switch(state) {
  241.         case OPTIONS:
  242.                 optionsScreen();
  243.                 break;
  244.         case SELECT:
  245.                 optionsScreen();
  246.                 break;
  247.         case LOGIN:
  248.                 loingScreen();
  249.                 break;
  250.         case REGISTER:
  251.                 doRegisterScreen();
  252.                 break;
  253.         case LOGGEDIN:
  254.                 optionsScreen();
  255.                 break;
  256.             default:
  257.                 optionsScreen();
  258.                
  259.         }
  260.     }
  261. }
  262.     class user {
  263.         private final String username;
  264.         private final String password;
  265.         public user(String username, String password) {
  266.             this.username = username;
  267.             this.password = password;
  268.             Main.usermap.add(this);
  269.         }
  270.         public String getPassword() {
  271.             return this.password;
  272.         }
  273.         public String getUsername() {
  274.             return this.username;
  275.         }
  276.     }
  277.     @SuppressWarnings("serial")
  278.     class PanelBG extends JPanel {
  279.          private Image backgroundImage;
  280.  
  281.           public PanelBG() {
  282.            /* try {
  283.                 backgroundImage = ImageIO.read(new File("/Users/georgeblair/Pictures/heic0503a.jpg"));
  284.             } catch (IOException e) {
  285.                 e.printStackTrace();
  286.             }
  287.             */
  288.           }
  289.  
  290.           public void paintComponent(Graphics g) {
  291.             super.paintComponent(g);
  292.             g.drawImage(backgroundImage, 0, 0, this);
  293.           }
  294.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement