2 Replies - 233 Views - Last Post: 08 February 2012 - 05:57 AM Rate Topic: -----

Topic Sponsor:

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-December 11

ActionListener: two buttons that print I was clicked n times

Posted 07 February 2012 - 11:26 PM

Enhance the ButtonViewer program so that it has two buttons, each of which prints a message “I was clicked n times!” whenever the button is clicked. Each button should have a separate click count.

Here is what I have for my Button Viewer. I don't know if I am doing this right. Would I have to do a new class actionlistener for each button? Also, how would I do the counter? It would be something like system.out.println("I was clicked" + n + "times!"); ??

Here is my code:

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
   This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{  
   private static final int FRAME_WIDTH = 100;
   private static final int FRAME_HEIGHT = 100;

   public static void main(String[] args)
   {  
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      
      JButton button1 = new JButton("Click me!");
      panel.add(button1);
      JButton button2 = new JButton("Click me!");
      panel.add(button2);
      
      
      class AddActionListener implements ActionListener
       { 
          public void actionPerformed(ActionEvent event)
          {
           
           
          }
       }
      frame.add(panel);
      
      ActionListener listener = new ClickListener();
      button1.addActionListener(listener);
      
      
      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener: two buttons that print I was clicked n times

#2 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: ActionListener: two buttons that print I was clicked n times

Posted 08 February 2012 - 12:49 AM

Why do you have a class inside your main method? It seems to be doing nothing. Anyway, you can use a single class for GUI action. For each button you can assign an ActionCommand .setActionCommand("CommandHere");, then in your actionPerformed() method you can use conditionals.

if (e.getActionCommand.equals("CommandHere"))
{
   // ... more code
}



For your counter, you simply need two variables [counter1 and counter2] and then in your class you can increment the appropriate value, and print a message as you wish. This is easiest with with an inner (or helper) class.

This post has been edited by Mylo: 08 February 2012 - 12:54 AM

Was This Post Helpful? 1
  • +
  • -

#3 javaHeyy  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 08-February 12

Re: ActionListener: two buttons that print I was clicked n times

Posted 08 February 2012 - 05:57 AM

I would simply add a counter for each button, just adding up until you exit, which is when you will have the output of both buttons being click i'm guessing. Or you can set it up so that every time you click the button, it updates the number of clicks in the actual button's text.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1