Advertisement
makispaiktis

5. Action Listeners

May 16th, 2022 (edited)
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4.  
  5. import javax.swing.JOptionPane;
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextField;
  8. import javax.swing.JPasswordField;
  9.  
  10.  
  11.  
  12. public class PasswordCreator extends JFrame{
  13.  
  14.     // Variables
  15.     private JTextField text1;
  16.     private JTextField text2;
  17.     private JTextField text3;
  18.     private JPasswordField pass;
  19.  
  20.     // Constructor
  21.     PasswordCreator(){
  22.  
  23.         // Basics
  24.         super("JFrame's title = Password Creation");
  25.         setLayout(new FlowLayout());
  26.         text1 = new JTextField(10);
  27.         text2 = new JTextField("Name: ");
  28.         text3 = new JTextField("Surname: ", 20);
  29.         pass = new JPasswordField("My password: ");
  30.         add(text1);
  31.         add(text2);
  32.         add(text3);
  33.         add(pass);
  34.  
  35.         // More
  36.         MyListener listener = new MyListener();
  37.         text1.addActionListener(listener);
  38.         text2.addActionListener(listener);
  39.         text3.addActionListener(listener);
  40.         pass.addActionListener(listener);
  41.     }
  42.  
  43.     // *********************************************************************************
  44.     // New class
  45.     // *********************************************************************************
  46.     private class MyListener implements ActionListener{
  47.  
  48.         // Includes only 1 method for listening to who was "pressed"
  49.         public void actionPerformed(ActionEvent event){
  50.             String str = "";
  51.             if(event.getSource() == text1)
  52.                 str = String.format("Text field 1: %s", event.getActionCommand());
  53.             else if(event.getSource() == text2)
  54.                 str = String.format("Text field 2: %s", event.getActionCommand());
  55.             else if(event.getSource() == text3)
  56.                 str = String.format("Text field 3: %s", event.getActionCommand());
  57.             else if(event.getSource() == pass)
  58.                 str = String.format("Pass field 1: %s", event.getActionCommand());
  59.  
  60.             JOptionPane.showMessageDialog(null, str);
  61.         }
  62.     }
  63.  
  64.  
  65.     // *********************************************************************************
  66.     // Main
  67.     // *********************************************************************************
  68.     public static void main(String args[]){
  69.         PasswordCreator frame = new PasswordCreator();
  70.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  71.         frame.setSize(600, 600);
  72.         frame.setVisible(true);
  73.     }
  74.  
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement