Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. package busTicketSellingSystem;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import static java.rmi.server.ObjID.read;
  12. import static java.rmi.server.UID.read;
  13. import java.util.Scanner;
  14. import javax.swing.*;
  15.  
  16. public class FrmLoginPage extends JFrame implements ActionListener {
  17.  
  18. JFrame frmLogin;
  19. private JLabel lblUsername, lblPassword, lblName;
  20. JButton btnLogin, btnSignUp, btnClear;
  21. JTextField txtUsername;
  22. JPasswordField txtPassword;
  23. private JLabel lblLogo;
  24. private static final String LOGO = "C:\\Users\\User\\Desktop\\Java Programming\\BusTicketSellingSystem\\Pics/bus.png";
  25. private JPanel pnlButton, pnlLogo;
  26.  
  27. public FrmLoginPage () {
  28.  
  29. frmLogin = new JFrame();
  30. frmLogin.setMinimumSize(new Dimension(800, 700));
  31. frmLogin.setLocationRelativeTo(null);
  32. frmLogin.setTitle("Login");
  33. frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. frmLogin.setLayout(new GridBagLayout());
  35. GridBagConstraints constraint = new GridBagConstraints();
  36. pnlButton = new JPanel();
  37. pnlButton.setLayout(new GridBagLayout());
  38.  
  39. lblUsername = new JLabel("Username:");
  40. lblUsername.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  41.  
  42. lblPassword = new JLabel("Password:");
  43. lblPassword.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  44.  
  45. lblName = new JLabel("Login Page");
  46. lblName.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  47.  
  48. txtUsername = new JTextField(100);
  49. txtUsername.setFont(new Font(Font.SANS_SERIF, Font.LAYOUT_LEFT_TO_RIGHT, 20));
  50. txtPassword = new JPasswordField(100);
  51. txtPassword.setFont(new Font(Font.SANS_SERIF, Font.LAYOUT_LEFT_TO_RIGHT, 20));
  52.  
  53. btnLogin = new JButton("Login");
  54. btnLogin.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  55.  
  56. btnSignUp = new JButton("Sign Up");
  57. btnSignUp.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  58.  
  59. btnClear = new JButton("Clear");
  60. btnClear.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
  61.  
  62. pnlLogo = new JPanel();
  63. pnlLogo.setLayout(new GridBagLayout());
  64.  
  65. lblLogo = new JLabel("", new ImageIcon(getScaledImage(new ImageIcon(LOGO).getImage(), 300, 250)), JLabel.CENTER);
  66.  
  67. constraint.gridx = 2;
  68. constraint.gridy = 0;
  69. frmLogin.add(pnlLogo, constraint);
  70.  
  71. constraint.insets = new Insets(0, 0, 40, 110);
  72. constraint.gridx = 0;
  73. constraint.gridy = 0;
  74. constraint.anchor = GridBagConstraints.CENTER;
  75. constraint.gridwidth = 1;
  76. constraint.gridheight = 1;
  77. pnlLogo.add(lblLogo, constraint);
  78.  
  79. constraint.gridx = 0;
  80. constraint.gridy = 2;
  81. constraint.gridwidth = 1;
  82. constraint.insets = new Insets(0, 50, 40, 10);
  83. constraint.anchor = GridBagConstraints.LINE_START;
  84. frmLogin.add(lblUsername, constraint);
  85.  
  86. constraint.gridy = 3;
  87. frmLogin.add(lblPassword, constraint);
  88.  
  89. constraint.gridx = 2;
  90. constraint.gridy = 2;
  91. constraint.anchor = GridBagConstraints.CENTER;
  92. constraint.fill = GridBagConstraints.HORIZONTAL;
  93. constraint.weightx = 1;
  94. constraint.ipady = 10;
  95. constraint.insets = new Insets(0, 0, 40, 50);
  96. frmLogin.add(txtUsername, constraint);
  97.  
  98. constraint.gridy = 3;
  99. frmLogin.add(txtPassword, constraint);
  100.  
  101. constraint.gridx = 0;
  102. constraint.gridy = 4;
  103. constraint.ipady = 0;
  104. constraint.ipadx = 0;
  105. constraint.insets = new Insets(0, 0, 0, 0);
  106. constraint.fill = GridBagConstraints.HORIZONTAL;
  107. constraint.anchor = GridBagConstraints.CENTER;
  108. constraint.gridwidth = 0;
  109. frmLogin.add(pnlButton, constraint);
  110.  
  111. constraint.gridx = 0;
  112. constraint.gridy = 1;
  113. constraint.insets = new Insets(0, 0, 0, 30);
  114. constraint.fill = GridBagConstraints.NONE;
  115. constraint.anchor = GridBagConstraints.CENTER;
  116. constraint.weightx = 0;
  117. constraint.weighty = 0;
  118. constraint.gridwidth = 1;
  119. constraint.ipadx = 60;
  120. constraint.ipady = 15;
  121. pnlButton.add(btnLogin, constraint);
  122.  
  123. constraint.gridx = 1;
  124. constraint.gridy = 1;
  125. constraint.ipadx = 60;
  126. constraint.ipady = 15;
  127. pnlButton.add(btnClear, constraint);
  128.  
  129. constraint.gridx = 2;
  130. constraint.gridy = 1;
  131. constraint.insets = new Insets(0, 0, 0, 0);
  132. constraint.ipadx = 60;
  133. constraint.ipady = 15;
  134. pnlButton.add(btnSignUp, constraint);
  135. frmLogin.setVisible(true);
  136. btnLogin.addActionListener(this);
  137. btnSignUp.addActionListener(this);
  138. btnClear.addActionListener(this);
  139. }
  140.  
  141. private static Image getScaledImage(Image srcImg, int w, int h) {
  142. BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.BITMASK);
  143. Graphics2D g2 = resizedImg.createGraphics();
  144. g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  145. g2.drawImage(srcImg, 0, 0, w, h, null);
  146. g2.dispose();
  147. return resizedImg;
  148.  
  149. }
  150.  
  151. public void actionPerformed(ActionEvent p){
  152. Object pressed = p.getSource();
  153.  
  154. if (pressed == btnClear){
  155. txtUsername.setText("");
  156. txtPassword.setText("");
  157. txtUsername.requestFocus();
  158. }
  159.  
  160.  
  161. if (pressed == btnLogin){
  162. txtUsername.setText("");
  163. txtPassword.setText("");
  164. }
  165.  
  166.  
  167. if (pressed == btnSignUp){
  168. txtUsername.setText("");
  169. txtPassword.setText("");
  170. }
  171.  
  172.  
  173. File loginf = new File("C:\\Users\\User\\Desktop\\Java Programming\\BusTicketSellingSystem\\Registration.txt");
  174. try{
  175. Scanner read = new Scanner(loginf);
  176. read.useDelimiter(",");
  177.  
  178. boolean login = false;
  179. while(read.nextLine() !=null){
  180. String user = read.next();
  181. String pass = read.next();
  182. read.next();
  183. if(txtUsername.getText().equals(user) && txtPassword.getText().equals(pass)){
  184. login = true;
  185. break;
  186. }
  187. }
  188. if(login)
  189. new FrmMainMenu();
  190. else {
  191. JOptionPane.showMessageDialog(null, "Incorrect username or password");
  192. txtUsername.setText("");
  193. txtPassword.setText("");
  194. }
  195. read.close();
  196. }
  197. catch (FileNotFoundException e){
  198. JOptionPane.showMessageDialog(null, "Can't find a text file");
  199. }
  200. }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement