Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JOptionPane;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- public class PasswordCreator extends JFrame{
- // Variables
- private JTextField text1;
- private JTextField text2;
- private JTextField text3;
- private JPasswordField pass;
- // Constructor
- PasswordCreator(){
- // Basics
- super("JFrame's title = Password Creation");
- setLayout(new FlowLayout());
- text1 = new JTextField(10);
- text2 = new JTextField("Name: ");
- text3 = new JTextField("Surname: ", 20);
- pass = new JPasswordField("My password: ");
- add(text1);
- add(text2);
- add(text3);
- add(pass);
- // More
- MyListener listener = new MyListener();
- text1.addActionListener(listener);
- text2.addActionListener(listener);
- text3.addActionListener(listener);
- pass.addActionListener(listener);
- }
- // *********************************************************************************
- // New class
- // *********************************************************************************
- private class MyListener implements ActionListener{
- // Includes only 1 method for listening to who was "pressed"
- public void actionPerformed(ActionEvent event){
- String str = "";
- if(event.getSource() == text1)
- str = String.format("Text field 1: %s", event.getActionCommand());
- else if(event.getSource() == text2)
- str = String.format("Text field 2: %s", event.getActionCommand());
- else if(event.getSource() == text3)
- str = String.format("Text field 3: %s", event.getActionCommand());
- else if(event.getSource() == pass)
- str = String.format("Pass field 1: %s", event.getActionCommand());
- JOptionPane.showMessageDialog(null, str);
- }
- }
- // *********************************************************************************
- // Main
- // *********************************************************************************
- public static void main(String args[]){
- PasswordCreator frame = new PasswordCreator();
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setSize(600, 600);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement