Advertisement
Guest User

Untitled

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