I'm being asked to transform an anonymous button class into an inner button class. The text I'm given to read discusses this topic using examples incorporating an ActonListener. However, the code I'm being asked to modify does not have an ActonListener in it. So I'm having difficulty following what I'm supposed to do. How do I take the following code and convert an anonymous to an inner class. does my button code even have an anonymous class in it?
Caveat: Don't just type up an answer for me. I need to learn this. Please help point me in the right direction.
Here's my code:
package ui.panels;
import java.awt.Panel;
import interfaces.Resettable;
import model.Model;
import ui.panels.ButtonPanel;
public class ControlsPanel extends Panel implements Resettable{
private ButtonPanel btnPanel;
public ControlsPanel (Model model) {
btnPanel = new ButtonPanel(model);
add(btnPanel);
}
public void resetComponents() {
}
}
And here is "ButtonPanel.java"
package ui.panels;
import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.Model;
public class ButtonPanel extends Panel {
private Button btnClear;
public ButtonPanel(final Model model) {
btnClear = new Button("Clear");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
model.resetComponents();
model.repaint();
}
});
add(btnClear);
}
}
ButtonPanel? It appears to be non-standard, which will make it tricky to lend advice. (Presumably it contains the actual buttons, but without seeing what it looks like, not sure what you should do.) Furthermore, it does not appear that you are using any inner classes, anonymous or otherwise, in the code you provided.new ActionListener() { ... }. I think you've just solved this yourself, so you might as well post your own answer below - thanks.