簡體   English   中英

在另一個類中使用ActionListener

[英]Using ActionListener in another class

因此,我知道已經回答了類似的問題,但是我的問題要更具體一些。 我正在編寫一個簡單的程序,比較兩個輸入的單詞,然后告訴您它們是否匹配。 這是代碼:

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();

    }

}

在另一類中,程序邏輯為:

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);

    }

}

}

所以,我的問題全是關於class Password ,在這一行:

PasswordEngine engine = new PasswordEngine(this);

為什么this是參數? 為什么該行實際上需要一個參數? 我不明白為什么this需要在那里。

PS:我是Java和編程的新手。

PasswordEngine類具有單個構造函數,該構造函數接受類型為Password的參數:

PasswordEngine(Password parent) {

因此,每當您說出new Password(…) ,…就必須對應於Password類型的對象(通常可能為null ,但這將在以后導致問題)。 並且由於該參數用於描述將對其應用操作的密碼小部件,因此密碼小部件將自己(讀取this )作為該參數。 這樣, PasswordEngine知道是哪個Password對象創建的。

請注意 ,許多應用程序都將嵌套類用於此操作。 嵌套類可以訪問其封閉類的成員,因此不需要顯式處理parent類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM