Anonymous class turned into an inner class

I am asked to convert an anonymous button class to an inner class. The text I am giving read discusses this topic using examples that include ActonListener . However, the code I am asked to change does not contain ActonListener . Therefore, I am having difficulty with what I should do. How to take the following code and convert anonymous to inner class. Does my button code even have an anonymous class?

Warning. Do not dial the answer for me. I need to find out. Please help point me in the right direction.

Here is 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 the "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); } } 
+4
source share
6 answers

Ah ha! I think the anonymous class is inside the "ButtonPanel.java" argh! I spent hours on the wrong file!

+1
source

The button is not anonymous, but an action listener. In particular:

 btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { model.resetComponents(); model.repaint(); } }); 

Here you call the addActionListener method and pass an ActionListener implementation as an argument. The key point here is that this implementation is an anonymous class that implements the inline interface.

Remember that an ActionListener is an interface, and interfaces cannot contain the actual implementation. Instead, what happens is that a new class is declared (without a name) that implements the ActionListener . This interface defines one actionPerformed method, which is the actual button handler - it calls resetComponents and repaint . You can turn this into a new normal class, which actually has a name, continues to implement ActionListener , implementing actionPerformed .

+4
source

Indeed, there is no anonymous class. One can imagine that transformations are performed in a different way, for example. new Resettable () ....

0
source

--- edited as the question is updated ---

 btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { model.resetComponents(); model.repaint(); } }); 

contains an anonymous class extended from ActionListener

You need this code to read something like

 btnClear.addActionListener(new MyInnerClass()); 

If you do this with everything that works 100%, I would say that you did it!

--- The original message follows --- The question is worded in such a way that I could understand it; but I'm not sure.

One hint is that he asked for an inner class. This means that the solution expects you to write an inner class:

 public class ControlsPanel extends Panel implements Resettable { // must have an inner class class SomeButtonModel (or SomeButtonPanel) { // code for inner class. } } 

Please note: if you find an abstract class (or interface) that is created in your code by specifying the missing functions in the initializer block, you probably found an β€œanonymous” class. This means that your inner class must extend this particular class, and you must redo the problem to use your inner class instead of the anonymous construct.

As usual, the main problem is to understand the question, once this is done, the rest just works.

0
source

Well ButtonPanel is non-standard, as far as I can see, so each answer will be complicated without additional information, but I can give some answers by default :-)

"does my button code even have an anonymous class?" No no. An anonymous class is often created if you want to get an instance of a class that implements a specific interface.

An example non-GUI will be a comparator. You want to compare an array by a certain criterion - you can create an additional class for it, but you will use it only once, and there is no real possibility of reuse. Therefore, we simply create an anonymous class that implements the Comparator interface:

  Comparator<Integer> c = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }; 

Nothing special about this question - note that your anonymous class can access external variables, but they are copied when the object is created. To avoid confusion, this means that local variables must be final.

0
source

An example of using an anonymous class will implement the ActionListener interface on the fly, so to speak, without defining a separate class (that is, a class with a name and, therefore, not anonymous). For instance:

 JButton jb = new JButton("click me"); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // do stuff } }); 

The "new ActionListener () class" is anonymous because it does not have a name.

To create an ActionListener as an inner class inside an outer class (called OuterClass), you should say something like:

 public class OuterClass { public OuterClass() { JButton jb = new JButton("click me"); jb.addActionListener(new MyActionListener()); } private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // do stuff } } // end inner class } // end outer class 

Now, instead of an anonymous class, you have a named inner class, namely "MyActionListener".

Hope that helps

Red

0
source

Source: https://habr.com/ru/post/1340101/


All Articles