简体   繁体   English

如何将对象的事件侦听器注册到其类之外的事件处理程序?

[英]How do I register an object's event listener to an event handler outside its class?

I have 2 classes, one JFrame class and one JButton class called LightsOutButton. 我有2个类,一个JFrame类和一个JButton类,称为LightsOutButton。 The JFrame class has a collection of these LightsOutButton objects. JFrame类具有这些LightsOutButton对象的集合。 I need to do actions when the individual buttons are pressed, but I can't access the JFrame class's methods from the event handler in my LightsOutButton class. 当按下各个按钮时,我需要执行操作,但是无法从LightsOutButton类中的事件处理程序访问JFrame类的方法。

It seems like the best way to work around this would be to register each button with the JFrame class's event handler. 解决此问题的最佳方法似乎是向JFrame类的事件处理程序注册每个按钮。 How would I do that? 我该怎么做?

`// this is my button object. It has registered its action listener to itself(which I want to change)
public class LightsOutButton extends JButton implements ActionListener {
private boolean onOrOff; // true = on, false = off
private int iCoord;
private int jCoord;

public LightsOutButton() {
    super.addActionListener(this);
    onOrOff = false;
    super.setContentAreaFilled(false);
    }
}

// this is my JFrame class. This is the class that I want to handle each button object's events

public void actionPerformed(ActionEvent e) {
    // Get the button that was clicked
    JButton button = (JButton)e.getSource();
    if(button == newGame) {
        reset();
    }
    if(button == manual) {
    if (manualMode == false) {
        manualMode = true;
    }
    if (manualMode == true) {
        manualMode = false;
    }
}
    // this is the implementation that I'm wishing for here:
    if(button = LightsOutButton){
        // do something
    }
}`

Basically as I understand it, you have a group of LightsOutButton which have been added to a JFrame . 基本上,据我了解,您已经将一组LightsOutButton添加到了JFrame In LightsOutButton you are trying to perform some action, but need to access the JFrame in some way to achieve it... LightsOutButton您尝试执行一些操作,但是需要以某种方式访问JFrame来实现它。

I would say you have the wrong concept. 我会说你有错误的概念。 Sure, there's no reason why LightsOutButton can't have it's own event handlers to manage it's internal state, but beyond that, it shouldn't be concerned about anybody else. 当然,没有理由为什么LightsOutButton不能拥有自己的事件处理程序来管理其内部状态,但是除此之外,它不应该关心其他任何人。

It seems like the best way to work around this would be to register each button with the JFrame class's event handle 解决此问题的最佳方法似乎是向JFrame类的事件句柄注册每个按钮

Would be an accurate assessment. 将是一个准确的评估。 How you do it will depend on how you've managed your code, but probably the simplest would be to add the LightsOutButton s to some kind of List or array and add and register the handler from within a loop. 如何执行将取决于您如何管理代码,但最简单的方法可能是将LightsOutButton添加到某种List或数组中,并在循环内添加和注册处理程序。

Identification of each light will be your next problem, but you could use the reference from the List /array, or set the name property of each light or the actionCommand property depending on your needs. 识别每个光源将是您的下一个问题,但是您可以使用List / array中的引用,也可以根据需要设置每个光源的name属性或actionCommand属性。

If each light has a "particular" task to do, you could consider using a Action or specialised ActionListener for each one, but that comes down to needs 如果每盏灯都有一个“特定的”任务要做,您可以考虑为每个灯使用一个Action或专用的ActionListener ,但这ActionListener需求

in your JFrame class, when you add you buttons 在您的JFrame类中,当您添加按钮时

LightsOutButton b = new LightsButton(..);
b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //you have acces to JFrame methods ..
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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