Guest User

Untitled

a guest
Dec 11th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class CreateAccount extends JFrame{
  7.  
  8. private JLabel user, pass; //label next to username and password field
  9. private JTextField username; //enter username
  10. private JPasswordField password; //enter password
  11. private JButton create;
  12. private String u, p;
  13. private static final int WIDTH = 500;
  14. private static final int HEIGHT = 300;
  15.  
  16. public CreateAccount(){
  17.  
  18.  
  19. setTitle("Create Account");
  20. setDefaultCloseOperation(EXIT_ON_CLOSE);
  21. setSize(WIDTH, HEIGHT);
  22. setMinimumSize(new Dimension(WIDTH, HEIGHT));
  23. setLocationRelativeTo(null);
  24. setResizable(false);
  25.  
  26. createView();
  27. setVisible(true);
  28.  
  29. }
  30.  
  31.  
  32. private void createView(){
  33.  
  34.  
  35. JPanel panelMain = new JPanel(null);
  36. getContentPane().add(panelMain);
  37.  
  38. user = new JLabel("Username: ");
  39. panelMain.add(user);
  40. user.setBounds(50, 30, 120, 25);
  41.  
  42. username = new JTextField();
  43. panelMain.add(username);
  44. username.setBounds(140, 30, 150, 25);
  45.  
  46.  
  47. pass = new JLabel("Password: ");
  48. panelMain.add(pass);
  49. pass.setBounds(50, 100, 120, 25);
  50.  
  51. password = new JPasswordField();
  52. panelMain.add(password);
  53. password.setBounds(140, 100, 150, 25);
  54.  
  55. create = new JButton("Create");
  56. panelMain.add(create);
  57. create.setBounds(170, 180, 100,25);
  58. create.addActionListener((new Listener()));
  59.  
  60. }
  61.  
  62. private class Listener implements ActionListener {
  63. @Override
  64. public void actionPerformed(ActionEvent e) {
  65. String usn = username.getText();
  66. String psd = password.getText();
  67. setUser(usn);
  68. setPass(psd);
  69. new Login();
  70. setVisible(false);
  71. }
  72. }
  73.  
  74. public void setUser(String usn){
  75. this.u = usn;
  76. }
  77.  
  78. public String getUser(){
  79. return u;
  80. }
  81.  
  82. public void setPass(String psd){
  83. this.p = psd;
  84. }
  85.  
  86. public String getPass(){
  87. return p;
  88. }
  89.  
  90. }
  91.  
  92. import javax.swing.*;
  93. import java.awt.*;
  94. import java.awt.event.ActionEvent;
  95. import java.awt.event.ActionListener;
  96.  
  97. public class Login extends JFrame{
  98.  
  99. private JLabel user, pass;
  100. private JTextField username;
  101. JPasswordField password;
  102. private JButton login;
  103. private static final int WIDTH = 500;
  104. private static final int HEIGHT = 300;
  105.  
  106.  
  107. public Login(){
  108.  
  109. setTitle("Login");
  110. setDefaultCloseOperation(EXIT_ON_CLOSE);
  111. setSize(WIDTH, HEIGHT);
  112. setMinimumSize(new Dimension(400, 200));
  113. setLocationRelativeTo(null);
  114. setResizable(false);
  115. createView();
  116. setVisible(true);
  117.  
  118. }
  119.  
  120. private void createView(){
  121.  
  122. JPanel panel = new JPanel(null);
  123. getContentPane().add(panel);
  124.  
  125. user = new JLabel("Username: ");
  126. panel.add(user);
  127. user.setBounds(50, 30, 120, 25);
  128.  
  129. username = new JTextField();
  130. panel.add(username);
  131. username.setBounds(140, 30, 150, 25);
  132.  
  133. pass = new JLabel("Password: ");
  134. panel.add(pass);
  135. pass.setBounds(50, 100, 120, 25);
  136.  
  137. password = new JPasswordField();
  138. panel.add(password);
  139. password.setBounds(140, 100, 150, 25);
  140.  
  141. login = new JButton("Login");
  142. panel.add(login);
  143. login.setBounds(170, 170, 100, 25);
  144. login.addActionListener((new Listener()));
  145. }
  146.  
  147. private class Listener implements ActionListener{
  148. CreateAccount grab = new CreateAccount();
  149. @Override
  150. public void actionPerformed(ActionEvent e) {
  151. String usn = username.getText();
  152. String psd = password.getText();
  153.  
  154. if(usn.equals("Admin") && psd.equals("Password")){
  155.  
  156. new AdminDB();
  157. }
  158.  
  159. else if (usn.equals(grab.getUser()) && psd.equals(grab.getPass()))
  160. {
  161. new UserDB();
  162. }
  163.  
  164. else
  165. {
  166. JOptionPane.showMessageDialog(null,"Invalid login info, try again");
  167. }
  168.  
  169. setVisible(false);
  170. }
  171. }
  172.  
  173.  
  174. }
Add Comment
Please, Sign In to add comment