Question
Programming Activity 12-1 Guidance ================================== Part 1 ------ The function you are to make your changes in is called JButtonPractice(). It is the constructor of
Programming Activity 12-1 Guidance ================================== Part 1 ------ The function you are to make your changes in is called JButtonPractice(). It is the constructor of the JButtonPractice class. Look near the top of it and you will see the following code: open = new JButton("OPEN"); contents.add(open); close = new JButton("CLOSE"); contents.add(close); You can see the 2 button objects there with the names "open" and "close". They are declared up higher in the class as follows: private JButton open; private JButton close; Remember that Java is case sensitive. "OPEN" is NOT the same as "open". addActionListener() is the function to use to register a button handler. It should be called via a JButton object and passed a reference to an object of your button handler class, which should be a private inner class that you define in part 2. The assignment notes instruct you to name this class ButtonHandler. The framework comments for part 1 tell you to declare and instantiate an object of your button handler and then register that object with each of the buttons (with object names "open" and "close"). Part 1 (cannot use "this") -------------------------- Consider: open.addActionListener(this); // This is not correct for our assignment. Look at the use of "this" here. It could be done this way IF you were allowed to change the framework code because you could then add an "implements" clause to the JButtonPractice class and then you could add an actionPerformed method to the JButtonPractice class. However, you MUST NOT change any framework code. You are only allowed to make your changes within the commented sections shown. In part 2, it tells you to code a private (inner) class to implement the listener. In part 1, you are to declare and instantiate an object of that private class and pass it (not "this") to each addActionListener() call. Part 2 ------ Here is the class declaration statement for your button handler private inner class: private class ButtonHandler implements ActionListener Your button handler class must implement the ActionListener interface. Therefore, you must code an actionPerformed() function within it. Here is this function's declaration statement: public void actionPerformed(ActionEvent e) Both open() and close() functions are provided by the framework. You can see them just after part 2 of the student code. They already make the calls that affect the circuit user interface. The open() function only opens; it does not close. Since your button handler class in part 2 is a private inner class, you do not need an object name to call these functions. Just call open() or close() as needed within your handler's actionPerformed() function.
=================================================
PLEASE FILL IN THE STUDENT CODE AREAS!
Start Of Code : JButtonPractice Class
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class JButtonPractice extends JFrame { Container contents; // GUI components private JButton open; private JButton close; private Circuit circuit;
private static JButtonPractice app; private boolean firstTime = true;
public JButtonPractice() { super("Choose your activity"); contents = getContentPane(); contents.setLayout(new FlowLayout());
circuit = new Circuit();
open = new JButton("OPEN"); contents.add(open); close = new JButton("CLOSE"); contents.add(close);
// ***** 1. Student code // declare and instantiate the button handler // and register it on the buttons // Part 1 student code starts here:
// Part 1 student code ends here.
setSize(500, 375); setVisible(true); }
// ***** 2. Student code // Code a private class to implement the correct Listener // and its required method // To open the switch, call the open method with the statement // open( ); // To close the switch, call the close method with the statement // close( ); // The last statement of the method should be // animate( ); // Part 2 student code starts here:
// Part 2 student code ends here.
public void open() { circuit.open(); }
public void close() { circuit.close(); }
private void animate() { try { repaint(); Thread.sleep(200); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void paint(Graphics g) { if (firstTime) { firstTime = false; } super.paint(g); circuit.draw(g); }
public static void main(String[] args) { app = new JButtonPractice(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
==============================================================
Output Pictures:
Example Programming Activity 12-1 Output Start of program Choose your activity OPEN CLOSE Powar source Light bulb Switch After clicking open: Choose your activity OPENCLOSE Power source Light bulb SwitchStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started