Advertisement
Guest User

Untitled

a guest
May 10th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. package javaapplication12;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import javax.swing.*;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPasswordField;
  13. import javax.swing.JTextField;
  14. import java.io.*;
  15. import java.util.Scanner;
  16.  
  17. public class LoginPage extends JFrame {
  18.  
  19. JTextField txtuserid;
  20. JTextField txtpn;
  21. JPasswordField txtpwd;
  22. JButton btnLogin;
  23. String username, password;
  24. JRadioButton b1, b2;
  25.  
  26. public LoginPage() {
  27. super("Log In");
  28. getContentPane().setLayout(null);
  29.  
  30. JLabel lblUserid = new JLabel("User ID:");
  31. lblUserid.setBounds(60, 190, 112, 24);
  32. getContentPane().add(lblUserid);
  33.  
  34. JLabel lblPassword = new JLabel("Password:");
  35. lblPassword.setBounds(60, 240, 103, 24);
  36. getContentPane().add(lblPassword);
  37.  
  38. JLabel lblIP = new JLabel("Port Number:");
  39. lblIP.setBounds(60, 290, 92, 24);
  40. getContentPane().add(lblIP);
  41.  
  42. txtuserid = new JTextField();
  43. txtuserid.setBounds(160, 190, 149, 30);
  44. getContentPane().add(txtuserid); // add textField to frame
  45. txtuserid.setColumns(10);
  46.  
  47. txtpwd = new JPasswordField();
  48. txtpwd.setBounds(160, 240, 150, 30);
  49. getContentPane().add(txtpwd); // add textField to frame
  50. txtpwd.setColumns(10);
  51.  
  52. txtpn = new JTextField();
  53. txtpn.setBounds(160, 290, 150, 30);
  54. getContentPane().add(txtpn); // add textField to frame
  55. txtpn.setColumns(10);
  56. txtpn.addKeyListener(new KeyAdapter() {
  57. public void keyTyped(KeyEvent e) {
  58. char c = e.getKeyChar();
  59. if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
  60. e.consume();
  61. }
  62. }
  63. });
  64.  
  65. btnLogin = new JButton("Login");
  66. btnLogin.setBounds(150, 400, 100, 20);
  67. getContentPane().add(btnLogin);
  68. btnLogin.addActionListener(new ActionListener() {
  69. @Override
  70. public void actionPerformed(ActionEvent e) {
  71. username = txtuserid.getText();
  72. password = txtpwd.getText();
  73. if (username.equals("") || password.equals("") || txtpn.getText().equals("")) {
  74. JOptionPane.showMessageDialog(null, "Please fill in the blank(s).");
  75. } else {
  76. boolean access = validation();
  77. if (access == true) {
  78. if (b1.isSelected()) {
  79. try{
  80. int portnumber = Integer.parseInt(txtpn.getText());
  81. setVisible(false);
  82. }
  83. catch(Exception ex)
  84. {
  85. System.out.println("Unable to open AgentClient: "+ex);
  86. }
  87. } else if (b2.isSelected()) {
  88. try{
  89. int portnumber = Integer.parseInt(txtpn.getText());
  90. new Client(portnumber);
  91. setVisible(false);
  92. }
  93. catch(Exception ex)
  94. {
  95. System.out.println("Unable to open Client: "+ex);
  96. }
  97. }
  98. }
  99. else
  100. {
  101. JOptionPane.showMessageDialog(null, "Incorrect Username & Password");
  102. }
  103. }
  104.  
  105. }
  106. });
  107.  
  108. JButton btnExit = new JButton("Exit");
  109. btnExit.setBounds(150, 450, 100, 20);
  110. getContentPane().add(btnExit);
  111. btnExit.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. System.exit(0);
  115. }
  116. });
  117.  
  118. ButtonGroup radiobutton = new ButtonGroup();
  119. b1 = new JRadioButton("Agent");
  120. b1.setBounds(75, 350, 100, 20);
  121. radiobutton.add(b1);
  122. b2 = new JRadioButton("Client");
  123. b2.setBounds(225, 350, 100, 20);
  124. radiobutton.add(b2);
  125. getContentPane().add(b1);
  126. getContentPane().add(b2);
  127.  
  128. setResizable(false);
  129. setVisible(true);
  130. setBounds(100, 100, 400, 600);
  131. setDefaultCloseOperation(EXIT_ON_CLOSE);
  132.  
  133. }
  134.  
  135. public static void main(String[] args) {
  136. EventQueue.invokeLater(new Runnable() {
  137. public void run() {
  138. try {
  139. LoginPage loginpage = new LoginPage();
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. });
  145. }
  146.  
  147. public boolean validation() {
  148. boolean valid = false;
  149. try {
  150. File file = new File("user.txt");
  151. Scanner input = new Scanner(file);
  152.  
  153. while (input.hasNext()) {
  154. String id = input.next();
  155. String pw = input.next();
  156. if (id.equalsIgnoreCase(username) && pw.equals(password)) {
  157. valid = true;
  158. return valid;
  159. }
  160. }
  161. } catch (Exception e) {
  162. System.out.println("Exception thrown in validation: " + e);
  163. }
  164. return valid;
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement