Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package TextField;
  2.  
  3.  
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9. import javax.swing.JPasswordField;
  10. import javax.swing.JOptionPane;
  11.  
  12. public class TextFieldFrame extends JFrame{
  13.  
  14. private JTextField textField1;
  15. private JTextField textField2;
  16. private JTextField textField3;
  17. private JPasswordField passwordField;
  18.  
  19.  
  20. public TextFieldFrame()
  21. {
  22. super("Testing JTextField and JPasswordField");
  23. setLayout(new FlowLayout() ); //configura o layout de frame
  24.  
  25.  
  26.  
  27. textField1 = new JTextField(10);
  28. add ( textField1 );
  29.  
  30.  
  31. textField2 = new JTextField( "Enter text here" );
  32. add( textField2 );
  33.  
  34.  
  35. textField3 = new JTextField( "Uneditable text field", 21);
  36. textField3.setEditable( false );
  37. add ( textField3 );
  38.  
  39.  
  40. passwordField = new JPasswordField( "Hiden text" );
  41. add( passwordField );
  42.  
  43.  
  44.  
  45. TextFieldHandler handler = new TextFieldHandler();
  46. textField1.addActionListener( handler );
  47. textField2.addActionListener( handler );
  48. textField3.addActionListener( handler );
  49. passwordField.addActionListener( handler );
  50. }
  51.  
  52.  
  53. private class TextFieldHandler implements ActionListener
  54. {
  55. @Override// processa eventos de campo de texto
  56. public void actionPerformed( ActionEvent event)
  57. {
  58. String string = ""; // declara string a ser exibida
  59.  
  60.  
  61. if (event.getSource() == textField1)
  62. string = String.format("textField1: %s",
  63. event.getActionCommand());
  64.  
  65.  
  66. else if (event.getSource() == textField2)
  67. string = String.format("textField2: %s",
  68. event.getActionCommand());
  69.  
  70.  
  71. else if (event.getSource() == textField3)
  72. string = String.format("textField3: %s",
  73. event.getActionCommand());
  74.  
  75.  
  76. else if (event.getSource() == passwordField)
  77. string = String.format("passwordField: %s",
  78. event.getActionCommand());
  79.  
  80.  
  81. JOptionPane.showMessageDialog(null, string);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement