Advertisement
apl-mhd

textField

Dec 16th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.JPasswordField;
  5. import javax.swing.JTextField;
  6. import javax.swing.JOptionPane;
  7.  
  8.  
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. public class TextField extends JFrame {
  14.  
  15.  
  16.     public final JTextField textField1;
  17.     public final JTextField textField2;
  18.     public final JTextField textField3;
  19.     public final JPasswordField passwordField;
  20.  
  21.  
  22.     public TextField(){
  23.  
  24.         super("Teseting jatextfiel");
  25.  
  26.         setLayout(new FlowLayout());
  27.  
  28.         textField1 = new JTextField(10);
  29.         add(textField1);
  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.         passwordField = new JPasswordField("Hidden text");
  40.         add(passwordField);
  41.  
  42.         TextFieldHandler ob = new TextFieldHandler();
  43.  
  44.         textField1.addActionListener(ob);
  45.         textField2.addActionListener(ob);
  46.         textField3.addActionListener(ob);
  47.         passwordField.addActionListener(ob);
  48.  
  49.  
  50.     }
  51.  
  52.  
  53.     public class TextFieldHandler  implements ActionListener {
  54.  
  55.         @Override
  56.         public void actionPerformed(ActionEvent e) {
  57.  
  58.             String string = "null";
  59.  
  60.             if(e.getSource() == textField1)
  61.  
  62.                 string = "texTfield1 "+ e.getActionCommand();
  63.  
  64.  
  65.             if(e.getSource() == textField2)
  66.  
  67.                 string = "texTfield2 "+ e.getActionCommand();
  68.  
  69.  
  70.  
  71.             if(e.getSource() == textField3)
  72.  
  73.                 string = "texTfield3 "+ e.getActionCommand();
  74.  
  75.  
  76.  
  77.             if(e.getSource() == passwordField)
  78.  
  79.                 string = "passwordField "+ e.getActionCommand();
  80.  
  81.  
  82.               JOptionPane.showMessageDialog(null,string);
  83.         }
  84.     }
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. package com.company;
  94.  
  95. public class Main {
  96.  
  97.  
  98.     public static void main(String[] args) {
  99.  
  100.  
  101.         TextField textField =  new TextField();
  102.         textField.setSize(350,100);
  103.         textField.setVisible(true);
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement