Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Test an inner class you\'ll experiment with inner and anonymous classes. Review

ID: 3792579 • Letter: T

Question

Test an inner class

you'll experiment with inner and anonymous classes.

Review the application.
Import the project named ch13 ex l InnerClass Tester that's stored in the ex starts folder. Review the code for the TestFrame class and the code for the ClickListener class. Note that these classes are stored in separate files.

Testfarm codes
package murach.test;

import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TestFrame extends JFrame {
   
    public TestFrame() {
        // code that sets up the frame
        this.setTitle("Test Frame");
        this.setSize(400, 100);
        this.setLocationByPlatform(true);       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        JPanel panel = new JPanel();
        this.add(panel);
       
        // code that creates the button and adds the listener
        JButton button1 = new JButton("Test Button");
        ActionListener listener = new ClickListener();
        button1.addActionListener(listener);

        // code that displays the frame
        panel.add(button1);
        this.setVisible(true);       
    }
}

The clickListener codes
package murach.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// the inner class that implements the listener
public class ClickListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("The button was clicked!");           
    }       
}


2. Run the application. It should display the Test Frame window. Then, click on the button in this window. It should display a message to the console.

Create an inner class
3, open the TestFrame and ClickListener classes and make the ClickListener class an inner class of the TestFrame class. To do that, you can cut most of the code from the ClickListener class and paste it into the TestFrame class.

4- Delete the public modifier from the declaration of the ClickListener class. add the import statement for the ActionEvent class, and make any other modification to this code that are necessary.
5- Delete the ClickListener,java file. At this point, the project should only 5. contain the TestFramejava file and the Main java file.
6- Run the application and click on the button. It should work as it did before.

Create an anonymous class
7- Open the TestFrame class and make the ClickListener the ClickListener class lass. To do that, can begin by cutting the code starting with the opening brace and ending with the closing brace.
8- Delete the declaration of the clickListener class, modify code so from the anonymous class, and make any other modifications to this are necessary.
9- Run the application and click on the button. It should work as it did before

Explanation / Answer

I have made main class inside TestFrame Class For testing.You can use your own Main Class

For part 3,4,5,6

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TestFrame extends JFrame {

public TestFrame() {
// code that sets up the frame
this.setTitle("Test Frame");
this.setSize(400, 100);
this.setLocationByPlatform(true);   
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
this.add(panel);

// code that creates the button and adds the listener
JButton button1 = new JButton("Test Button");
ActionListener listener = new ClickListener();
button1.addActionListener(listener);

// code that displays the frame
panel.add(button1);
this.setVisible(true);   
  
}
class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked!");   
}
}
public static void main(String[] args) {
       new TestFrame();
   }
}

For part 7,8,9

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TestFrame extends JFrame {

public TestFrame() {
// code that sets up the frame
this.setTitle("Test Frame");
this.setSize(400, 100);
this.setLocationByPlatform(true);   
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
this.add(panel);

// code that creates the button and adds the listener
JButton button1 = new JButton("Test Button");
ActionListener listener = new ActionListener() {
          
           @Override
           public void actionPerformed(ActionEvent e) {
               System.out.println("The button was clicked!");
              
           }
       };
button1.addActionListener(listener);

// code that displays the frame
panel.add(button1);
this.setVisible(true);   
  
}
  
public static void main(String[] args) {
       new TestFrame();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote