Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions