Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I'm writing a Java program that is supposed to count the number of times each of the two buttons in the code is clicked.

Hi, I'm writing a Java program that is supposed to count the number of times each of the two buttons in the code is clicked. However, I am unable to run the code and get these counters to work. Could you please review and help me debug my code?

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.JLabel;

class SeparateActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.out.println("SeparateActionListener::actionPerformed!");

}

}

class ActionListenerPlus extends ButtonFrame implements ActionListener {

// Counts the number of clicks for button 1

private int counterOne = 0;

// Label to display counter for button 1

private JLabel buttonOneCountLabel;

// Button one

private JButton buttonOne;

// Counts the number of clicks for button 2

private int counterTwo = 0;

// Label to display counter for button 2

private JLabel buttonTwoCountLabel;

// Button 2

private JButton buttonTwo;

public ButtonFrame() {

setBounds(100,100,400,300); // Border for JFrame

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Closes window

// Creates a panel that holds the button and the label

JPanel myButtonClickPanel = new JPanel(); // Creates a new JPanel and we put buttons in here

myButtonClickPanel.setLayout(new FlowLayout());

JButton buttonOne = new JButton("Button 1"); // Creates button1

buttonOne.addActionListener(this); // "this" passes in JFrame, JFrame implements ActionListener

myButtonClickPanel.add(buttonOne); // Adds button1 to JPanel

JButton buttonTwo = new JButton("Button 2"); // Creates button2

buttonTwo.addActionListener(new SeparateActionListener()); // Typically, you only want one implementation of an ActionListener

myButtonClickPanel.add(buttonTwo); // Adds button2 to JPanel

myButtonClickPanel.add(buttonOne); // Adds button1 to JPanel

myButtonClickPanel.add(buttonTwo); // Adds button2 to JPanel

Container contentPane = getContentPane(); // Gets the container from the ButtonFrame - Method of JFrame

contentPane.setLayout(new BorderLayout()); // JFrame is a BorderLayOut

contentPane.add(myButtonClickPanel, BorderLayout.SOUTH); // Adds myButtonPanel to JFrame;

}

// This print out the message myButtonFrame::actionPerformed when button1 is clicked

public void actionPerformed(ActionEvent e) {

if (event.getSource() == buttonOne) {

counterOne++;

buttonOneCountLabel.setText("button1 was clicked " + counterOne + "times.");

}

else {

counterTwo++;

buttonTwoCountLabel.setText("button2 was clicked " + counterTwo + "times.");

}

}

public class ActionListenerPlus {

public static void main(String[] args) {

System.out.println("Hello ButtonClick!");

ButtonFrame myButtonFrame = new ButtonFrame(); // Creates new ButtonFrame object

myButtonFrame.setVisible(true); // Displays the window

}

}

}

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