简体   繁体   中英

Using ActionListener in another class

So, I know that similar question has been answered already, but mine is a bit more specific. I am writing a simple program that compares two entered words and then tells you if they match or not. Here is the code:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class Password {
JPanel windowContent, p1, p2, p3; //p1 is in the west, p2 in center and p3 in south
JButton check, clear;
JTextField pass1, pass2;
JLabel word1, word2, result;
JFrame frame;


Password() {
    windowContent = new JPanel();
    BorderLayout bl = new BorderLayout();
    windowContent.setLayout(bl);

    p1 = new JPanel();
    GridLayout gl = new GridLayout(2, 1);
    p1.setLayout(gl);

    word1 = new JLabel("1st word: ");
    word2 = new JLabel ("2nd word: ");

    p1.add(word1);
    p1.add(word2);

    windowContent.add("West", p1);

    p2 = new JPanel();
    p2.setLayout(gl);

    pass1 = new JTextField (20);
    pass2 = new JTextField (20);

    p2.add(pass1);
    p2.add(pass2);

    windowContent.add("Center", p2);

    p3  = new JPanel();
    FlowLayout fl = new FlowLayout();
    p3.setLayout(fl);

    check = new JButton("Check");
    clear = new JButton("Clear");
    result = new JLabel("");

    p3.add(check);
    p3.add(result);
    p3.add(clear);

    windowContent.add("South", p3);

    frame = new JFrame("Password");
    frame.add(windowContent);
    frame.pack();
    frame.setVisible(true);

    PasswordEngine engine = new PasswordEngine(this); // <--- THIS LINE HERE!

    check.addActionListener(engine);
    clear.addActionListener(engine);

    }

    public static void main(String [] args) {
        Password pass = new Password();

    }

}

And in another class the program logic:

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PasswordEngine implements ActionListener {

Password parent;
String textField1, textField2;

PasswordEngine(Password parent) {
    this.parent = parent;
}

public void actionPerformed(ActionEvent e) {
    String displayFieldText1 = parent.pass1.getText();
    String displayFieldText2 = parent.pass2.getText();

    Object src  = e.getSource();

    if (src == parent.clear) {
        parent.pass1.setText("");
        parent.pass2.setText("");
        parent.result.setText("");
        parent.pass1.setBackground(Color.white);
        parent.pass2.setBackground(Color.white);
    } else  if (displayFieldText1.equals(displayFieldText2)) {
        parent.result.setText("MATCH");
        parent.pass1.setBackground(Color.GREEN);
        parent.pass2.setBackground(Color.GREEN);
    } else {
        parent.result.setText("NOT MATCH");
        parent.pass1.setBackground(Color.white);
        parent.pass2.setBackground(Color.white);

    }

}

}

So, my question is all about class Password , at this line:

PasswordEngine engine = new PasswordEngine(this);

Why is this a parameter? Why does that line actualy need a parameter? I do not understand why this it needs to be there.

PS: I am new to Java and programming.

The PasswordEngine class has a single constructor, which takes an argument of type Password :

PasswordEngine(Password parent) {

So whenever you say new Password(…) then the … must correspont to an object of type Password (in general possibly null , but that would lead to problems later on). And as the parameter is used to describe the password widget to which the action will be applied, the password widget will pas itself (read this ) as that argument. This way, the PasswordEngine knows which Password object created it.

Note that many applications will use a nested class for this action. A nested class can access members of its enclosing class, so the explicit handling of parent would not be neccessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM