Guest User

Untitled

a guest
Aug 7th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. Java username password login simple
  2. import java.awt.BorderLayout;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.GridLayout;
  6. import java.awt.Insets;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JTextField;
  16.  
  17. public class PasswordForm
  18. {
  19. private static String password = "mypass";
  20. public static void main(String[] args)
  21. {
  22. // Basic form create
  23. JFrame frame = new JFrame("Form 1");
  24. frame.setSize(300,300);
  25. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26.  
  27. // Creating the grid
  28. JPanel panel = new JPanel(new GridBagLayout());
  29. frame.getContentPane().add(panel, BorderLayout.NORTH);
  30. GridBagConstraints c = new GridBagConstraints();
  31.  
  32. // Create some elements
  33. JTextField usernameInput = new JTextField(10);
  34. c.gridx = 0;
  35. c.gridy = 1;
  36. panel.add(usernameInput,c);
  37.  
  38. JPasswordField passwordInput = new JPasswordField(10);
  39. c.gridx = 0;
  40. c.gridy = 2;
  41. panel.add(passwordInput,c);
  42.  
  43. JButton loginInput = new JButton("Login");
  44. c.gridx = 0;
  45. c.gridy = 3;
  46. loginInput.addActionListener(new LoginButton());
  47. panel.add(loginInput,c);
  48.  
  49.  
  50. frame.setVisible(true);
  51. }
  52.  
  53. static class LoginButton implements ActionListener
  54. {
  55. public void actionPerformed(ActionEvent e)
  56. {
  57. JTextField usernameInput = (JTextField)e.getSource();
  58. JOptionPane.showMessageDialog(null,"Text is:");
  59. }
  60. }
  61. }
  62.  
  63. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton
  64. at PasswordForm$LoginButton.actionPerformed(PasswordForm.java:56)
  65. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  66. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  67. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  68. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  69. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  70. at java.awt.Component.processMouseEvent(Unknown Source)
  71. at javax.swing.JComponent.processMouseEvent(Unknown Source)
  72. at java.awt.Component.processEvent(Unknown Source)
  73. at java.awt.Container.processEvent(Unknown Source)
  74. at java.awt.Component.dispatchEventImpl(Unknown Source)
  75. at java.awt.Container.dispatchEventImpl(Unknown Source)
  76. at java.awt.Component.dispatchEvent(Unknown Source)
  77. at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  78. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  79. at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  80. at java.awt.Container.dispatchEventImpl(Unknown Source)
  81. at java.awt.Window.dispatchEventImpl(Unknown Source)
  82. at java.awt.Component.dispatchEvent(Unknown Source)
  83. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  84. at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
  85. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  86. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  87. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  88. at java.awt.EventDispatchThread.run(Unknown Source)
  89.  
  90. public class PasswordForm {
  91.  
  92. private static String password = "mypass";
  93. private JTextField usernameInput;
  94.  
  95. public PasswordForm() {
  96. }
  97.  
  98. private void init(){
  99. // Basic form create
  100. JFrame frame = new JFrame("Form 1");
  101. frame.setSize(300,300);
  102. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103.  
  104. // Creating the grid
  105. JPanel panel = new JPanel(new GridBagLayout());
  106. frame.getContentPane().add(panel, BorderLayout.NORTH);
  107. GridBagConstraints c = new GridBagConstraints();
  108.  
  109. // Create some elements
  110. usernameInput = new JTextField(10);
  111. c.gridx = 0;
  112. c.gridy = 1;
  113. panel.add(usernameInput,c);
  114.  
  115. JPasswordField passwordInput = new JPasswordField(10);
  116. c.gridx = 0;
  117. c.gridy = 2;
  118. panel.add(passwordInput,c);
  119.  
  120. JButton loginInput = new JButton("Login");
  121. c.gridx = 0;
  122. c.gridy = 3;
  123. loginInput.addActionListener(new LoginButton());
  124. panel.add(loginInput,c);
  125.  
  126.  
  127. frame.setVisible(true);
  128. }
  129. public static void main(String[] args){
  130. PasswordForm form = new PasswordForm();
  131. form.init();
  132. }
  133.  
  134. class LoginButton implements ActionListener{
  135.  
  136. public void actionPerformed(ActionEvent e){
  137. //JTextField usernameInput = (JTextField)e.getSource();
  138. String username = (usernameInput.getText().length()>0?usernameInput.getText():" U have not entered!");
  139. JOptionPane.showMessageDialog(null,"Text is : "+username);
  140. }
  141. }
  142. }
  143.  
  144. Exception in thread "AWT-EventQueue-0" java.lang.Error:
  145. Unresolved compilation problem:
  146. The method getText() is undefined for the type ActionEvent
  147.  
  148. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JTextField
  149. at StupidCode$loginButton.actionPerformed(PasswordForm.java:54)
  150. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  151. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  152. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  153. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  154. ...more stack trace information that I'm not sure will help at this stage
  155.  
  156. private static JPasswordField passwordInput;
  157. //main method below
  158. //...
  159. //main method finished, action listener follows...
  160.  
  161. JOptionPane.showMessageDialog(null,"Text is: "+ new String(passwordInput.getPassword()));
  162.  
  163. public class PasswordForm {
  164. private static String password = "mypass";
  165. public static void main(String[] args){
  166. //Swing operations should happen on the EDT
  167. EventQueue.invokeAndWait( new Runnable(){
  168. public void run(){
  169. //whole UI creation
  170. final JTextField usernameInput = new JTextField(10);
  171. final JPasswordField passwordInput = new JPasswordField(10);
  172. //more UI creation
  173. JButton loginInput = new JButton("Login");
  174. loginInput.addActionListener(new ActionListener(){
  175. public void actionPerformed(ActionEvent e){
  176. JOptionPane.showMessageDialog(null,"Username is:" + usernameInput.getText() + " Password is:" + passwordInput.getText());
  177. }
  178. });
  179. }
  180. } //todo catch the exceptions from the invokeAndWait call
  181. }
  182. }
Add Comment
Please, Sign In to add comment