Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To write an ActionListener class, follow the steps given below: 1. Declare an ActionListener class and specify that the class implements a listener interface. public

To write an ActionListener class, follow the steps given below: 1. Declare an ActionListener class and specify that the class implements a listener interface. public class ListenerClass implements ActionListener{} 2. Include the customized code that implements the method(s) in the ActionListener interface. public void actionPerformed (ActionEvent event) {} 3. Identify the JComponent(s) (action event sources) that you would like to emit the events. For example, a JComponent object or reference variable such as componentRef, can be used to refer to a panel, label, slider, check box, etc. 4. Register an instance of the ActionListener class as a listener on one or more of the source component(s) reference(s). componentRef.addActionListener(instanceOfListenerClass); 5. Add the source component to the Container reference variable (containerRef) or object that will display it. containerRef.add (componentRef);

Directions 1. Create an ActionListener class called VoteCounter that extends JPanel and implements ActionListener. Note: This class will inherit all the methods of JPanel and also implement the method actionPerformed for an ActionListener. 2. From the swing package import the following JComponents: JButton, JLabel, JPanel. 3. From the Abstract Window Toolkit (AWT) package import the following Color class. 4. From the Abstract Window Toolkit (AWT) Event package (java.awt.event) import the following ActionEvent and ActionListener class and interface. 5. (VoteCounter) Create the following data members: a. a private static final long class constant serialVersionUID equal to 1L. b. two private integer variables named yesVotes and noVotes that represents a counter for the number of yes votes and no votes. c. two private JButton reference variables yesButton and noButton. d. two private JLabel reference variables yesLabel and noLabel. 6. (VoteCounter) implement actionPerformed method a. in actionPerformed increment yesVotes using the increment operator. b. access the setText method of JLabel reference variable yesLabel (using the dot notation) and pass Yes Votes: and the yesVotes counter as arguments. 7. (VoteCounter) Create the following constructor that uses the inherited JPanel methods: a. Use the standard Java class organization convention by placing the constructor after data members. b. initialize the yesVotes and noVotes both to zero. c. JButton(s) : each JButton constructor can be initialized with a string of text, i. pass Yes to aJButton constructor and assign it to yesButton. ii. pass No to a JButton constructor and assign it to noButton. d. JLabel(s) i. pass Yes Votes: and the yesVotes counter as an argument in the JLabel constructor and assign it to yesLabel. ii. pass No Votes: and the noVotes counter as an argument in the JLabel constructor and assign it to noLabel. e. add an ActionListener detector to the yesButton: i. we will only add a signal detector to the yes button, to demonstrate how signals are detected when a JComponent button (source) is pressed. ii. the no button will demonstrate what happens when a detector is not added to a source component, i.e. nothing should happen. iii. call the addActionListener method of the JButton reference variable, yesButton and pass it the current subclass of ActionListener. f. set the Background color with the inherited method setBackground color to LIGHT_GRAY. g. add Components to JPanel: i. add the yesButton and the yesLabel to the content panel using the add method. ii. add the noButton and the noLabel to the content panel using the add method. 8. (VoteDisplay) Create a class called VoteDisplay that has a main method that does the following: a. creates a VoteCounter reference variable v and assigns a new VoteCounter object to v. b. creates a JFrame reference variable f and assigns a new JFrame object to f. c. add the VoteCounter panel with its ActionListener to v to f. d. call the setTitle method. The title should be set to "Vote Count". e. call the setSize method. The size of the frame should be set to 300 wide and 100 high. f. call the setLocation method. Set the JFrame location on the computer screen 750 right and 100 down. g. ensure that the program will terminate when the frame is closed, by calling its setDefaultCloseOperation method with the constant Jframe.EXIT_ON_CLOSE as its argument. Note: The program terminates when the close box x is clicked. h. make the window frame appear on your screen, by calling the setVisible method and assigning its argument to true.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions