Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TestFrame extends JFrame {
  6.  
  7. private PassWordDialog passDialog;
  8.  
  9. public TestFrame() {
  10. passDialog = new PassWordDialog(this, true);
  11. passDialog.setVisible(true);
  12. }
  13.  
  14. public static void main(String[] args) {
  15. SwingUtilities.invokeLater(new Runnable() {
  16. @Override
  17. public void run() {
  18. JFrame frame = new TestFrame();
  19. frame.getContentPane().setBackground(Color.BLACK);
  20. frame.setTitle("Logged In");
  21. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. frame.setLocationRelativeTo(null);
  23. frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  24. }
  25. });
  26. }
  27. }
  28.  
  29. class PassWordDialog extends JDialog {
  30.  
  31. private final JLabel jlblUsername = new JLabel("Username");
  32. private final JLabel jlblPassword = new JLabel("Password");
  33.  
  34. private final JTextField jtfUsername = new JTextField(15);
  35. private final JPasswordField jpfPassword = new JPasswordField();
  36.  
  37. private final JButton jbtOk = new JButton("Login");
  38. private final JButton jbtCancel = new JButton("Cancel");
  39.  
  40. private final JLabel jlblStatus = new JLabel(" ");
  41.  
  42. public PassWordDialog() {
  43. this(null, true);
  44. }
  45.  
  46. public PassWordDialog(final JFrame parent, boolean modal) {
  47. super(parent, modal);
  48.  
  49. JPanel p3 = new JPanel(new GridLayout(2, 1));
  50. p3.add(jlblUsername);
  51. p3.add(jlblPassword);
  52.  
  53. JPanel p4 = new JPanel(new GridLayout(2, 1));
  54. p4.add(jtfUsername);
  55. p4.add(jpfPassword);
  56.  
  57. JPanel p1 = new JPanel();
  58. p1.add(p3);
  59. p1.add(p4);
  60.  
  61. JPanel p2 = new JPanel();
  62. p2.add(jbtOk);
  63. p2.add(jbtCancel);
  64.  
  65. JPanel p5 = new JPanel(new BorderLayout());
  66. p5.add(p2, BorderLayout.CENTER);
  67. p5.add(jlblStatus, BorderLayout.NORTH);
  68. jlblStatus.setForeground(Color.RED);
  69. jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);
  70.  
  71. setLayout(new BorderLayout());
  72. add(p1, BorderLayout.CENTER);
  73. add(p5, BorderLayout.SOUTH);
  74. pack();
  75. setLocationRelativeTo(null);
  76. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  77.  
  78. addWindowListener(new WindowAdapter() {
  79. @Override
  80. public void windowClosing(WindowEvent e) {
  81. System.exit(0);
  82. }
  83. });
  84.  
  85.  
  86. jbtOk.addActionListener(new ActionListener() {
  87. @Override
  88. public void actionPerformed(ActionEvent e) {
  89. if ("stackoverflow".equals(String.valueOf(jpfPassword.getPassword()))
  90. && "stackoverflow".equals(jtfUsername.getText())) {
  91. parent.setVisible(true);
  92. setVisible(false);
  93. } else {
  94. jlblStatus.setText("Invalid username or password");
  95. }
  96. }
  97. });
  98. jbtCancel.addActionListener(new ActionListener() {
  99. @Override
  100. public void actionPerformed(ActionEvent e) {
  101. setVisible(false);
  102. parent.dispose();
  103. System.exit(0);
  104. }
  105. });
  106. }
  107. }
  108.  
  109. public class MyApp {
  110.  
  111. private static boolean isAuthenticated;
  112.  
  113. public static void main( String args[] ) {
  114. isAuthenticated = false;
  115. while( !isAuthenicated ) {
  116. authenticateUser();
  117. try{ Thread.sleep( 200 ); } catch( InterruptedException ie ){ }
  118. }
  119.  
  120. new JFrame();
  121. // finish rest of GUI code.
  122. }
  123.  
  124. private static void authenticateUser(){
  125. dialog = new MyAuthenticationDialog();
  126. dialog.show();
  127. if( isValid( dialog.getUsername(), dialog.getPassword() )
  128. isAuthenticated = true;
  129. else
  130. isAuthenticated = false;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement