简体   繁体   中英

Using ActionListener in a different class

I have an exercise in my Computer science class where i am learning to use Action listeners in Java Applets. Though I'm slightly confused and could do with some help! Here is the exercise that i have to perform:

"Provide a single button handler class for the three buttons. The constructor should take as its arguments the reference to the applet and an appropriate integer value for the size. Add addActionListener calls to the init method (ie each should create a new handler object dedicated to the particular button). Compile and run your applet."

Iv created a button handler class and created the constructor (i think i done it correctly). My main problem is the "add addActionListener calls to the init method" part.

Here is the ButtonHandler class i created:

      class ButtonHandler implements ActionListener {

        Square theApplet;

        ButtonHandler(Square app){
            theApplet = app;
        }
 }

After that i tried to add an addActionListener in the init method that the buttonhandler has to call to.

     butSmall.addActionListener(ButtonHandler());

But this gives me errors. Anyone that could explain how to complete this exercise ? Thank you.

Iv created a button handler class and created the constructor (i think i done it correctly).

Well no, you haven't done it correctly. The class declares that it implements ActionListener , but it does not actually provide an implementation for ActionListener.actionPerformed(ActionEvent) . It could get away with that if it were abstract , but it is not. (And if it were abstract then you would not be able to instantiate it.)

After that

I doubt it. The class you presented will not compile, so you did not get past that point with that class.

i tried to add an addActionListener in the init method that the buttonhandler has to call to.

  butSmall.addActionListener(ButtonHandler()); 

But this gives me errors.

It would be to your advantage to specify the error(s) you receive, and from where they come (ie from the compiler, in this case). If the compiler even gets to the point of trying to analyze that line, it would likely complain that the ButtonHandler constructor you are trying to invoke does not exist. The one constructor you presented requires an argument of type Square , but you are trying to invoke a constructor that takes no arguments.

Anyone that could explain how to complete this exercise ?

If you are trying to invoke that constructor from within the init() method of a Square , and you want to pass a reference to the Square whose init() method is performing the invocation, then you can use the keyword this as the argument. Again, however, before you can even get to that point you need to fix ButtonHandler .

Just create a class implementing ActionListener , specifically the actionPerformed(ActionEvent) function. If you need to specify parameters, create a custom constructor with the parameters you wish. Also, your ButtonHandler class is not static, so you need to initialize it with new .

Check my example below:

Its a listener that takes a JPanel and A JFrame as parameters and adds the JPanel to the JFrame as its content pane:

public class OpenScreenListener implements ActionListener {

private JPanel panel;
private JFrame window;
@Override
public void actionPerformed(ActionEvent e) {
    window.setContentPane(panel);
    window.setVisible(true);
}

public OpenScreenListener(JPanel panel, JFrame window){
    this.screen = panel
    this.window = window;
}

}

And this is how I add it to a button:

myButton.addActionListener(new OpenScreenListener(new JPanel("myJPanel"), myJframe));

If you look at the JavaDoc of ActionListener interface you see that it requires you to implement a method void actionPerformed(ActionEvent e) . Your custom ButtonHandler doesn't yet implement that method therfore it doesn't fullfill the contract that the ActionListener interface provides.

Add a block like this:

@Override
public void actionPerformed(ActionEvent e) {
    //Do something here
}

to your class.

Note that in most cases small ActionListener like click listeners for Buttons are implemented as anonymous functions. There is a nice "official" tutorial on how to write ActionListeners, make sure to read it!

To actuall instantiate a new instance of your ButtonHandler you need the Java keyword new like new ButtonHandler() .

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