Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5.  
  6. public class javaTesting extends JFrame {
  7. public JFrame mrFrame;
  8. public int enter;
  9. public JPanel mrPanel;
  10.  
  11. public javaTesting() throws Exception
  12. {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. try {
  15. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
  16. } catch (SQLException e){
  17. System.out.println(e.getMessage());
  18. }
  19. mrFrame = new JFrame();
  20. mrPanel = new JPanel();
  21.  
  22. mrPanel.setLayout(new GridLayout(4,1));
  23.  
  24. JLabel user = new JLabel("Username");
  25. mrPanel.add(user);
  26.  
  27. JTextField user_input = new JTextField(30);
  28. mrPanel.add(user_input);
  29.  
  30. JLabel pass = new JLabel("Password");
  31. mrPanel.add(pass);
  32.  
  33. JTextField pw_input = new JPasswordField(30);
  34. mrPanel.add(pw_input);
  35.  
  36. mrFrame.setSize(700,700);
  37. mrFrame.setLocationRelativeTo(null);
  38. mrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. //mrFrame.setVisible(true);
  40. mrFrame.setResizable(false);
  41.  
  42. input();
  43.  
  44. if(enter == JOptionPane.OK_OPTION)
  45. {
  46. JOptionPane.showMessageDialog(null, "You clicked ok!");
  47. input();
  48. } else {
  49. System.exit(1);
  50. }
  51. }
  52. public void input()
  53. {
  54. enter = (int) JOptionPane.showConfirmDialog(mrFrame,mrPanel,"Login Cridantiels",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
  55. }
  56. public static void main(String agrs[]) throws Exception
  57. {
  58. new javaTesting();
  59. }
  60. }
  61.  
  62. do{
  63. input();
  64.  
  65. if(enter == JOptionPane.OK_OPTION)
  66. {
  67. JOptionPane.showMessageDialog(null, "You clicked ok!");
  68. } else {
  69. System.exit(1);
  70. }
  71. } while(enter != JOptionPane.CANCEL_OPTION);
  72.  
  73. JPanel pnlDetails = new JPanel(new GridBagLayout());
  74. JTextField userNameField = new JTextField(10);
  75. JPasswordField passwordField = new JPasswordField(10);
  76.  
  77. GridBagConstraints gbc = new GridBagConstraints();
  78. gbc.gridx = 0;
  79. gbc.gridy = 0;
  80. gbc.anchor = GridBagConstraints.WEST;
  81. gbc.insets = new Insets(2, 2, 2, 2);
  82.  
  83. pnlDetails.add(new JLabel("User name:"), gbc);
  84. gbc.gridy++;
  85. pnlDetails.add(new JLabel("Password:"), gbc);
  86.  
  87. gbc.gridx = 1;
  88. gbc.gridy = 0;
  89. gbc.anchor = GridBagConstraints.EAST;
  90. pnlDetails.add(userNameField, gbc);
  91. gbc.gridy++;
  92. pnlDetails.add(passwordField, gbc);
  93.  
  94. // The result from the dialog, will be OK or CANCEL
  95. int operation;
  96. // Flag used to determine if the credentials are okay or not
  97. boolean badCredentials = true;
  98. do {
  99.  
  100. operation = JOptionPane.showConfirmDialog(null, pnlDetails, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
  101.  
  102. String userName = userNameField.getText();
  103. char[] password = passwordField.getPassword();
  104.  
  105. // You would valid you credintals here :P
  106. if (userName.equals("super") && new String(password).equals("happy")) {
  107.  
  108. badCredentials = false;
  109.  
  110. } else if (operation != JOptionPane.CANCEL_OPTION) {
  111.  
  112. JOptionPane.showMessageDialog(null, "Invalid Credentials", "Error", JOptionPane.ERROR_MESSAGE);
  113.  
  114. }
  115.  
  116. } while (operation != JOptionPane.CANCEL_OPTION && badCredentials);
  117.  
  118. if (!badCredentials && operation != JOptionPane.CANCEL_OPTION) {
  119.  
  120. System.out.println("Continue program");
  121.  
  122. } else {
  123.  
  124. System.out.println("Exit program");
  125.  
  126. }
  127.  
  128. System.exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement