简体   繁体   中英

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. The JFrame class has a collection of these LightsOutButton objects. 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.

It seems like the best way to work around this would be to register each button with the JFrame class's event handler. 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 . In LightsOutButton you are trying to perform some action, but need to access the JFrame in some way to achieve it...

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.

It seems like the best way to work around this would be to register each button with the JFrame class's event handle

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.

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.

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

in your JFrame class, when you add you buttons

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

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