Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: OK_Cancel_Button

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question OK_Cancel_Button

    I have it working to show that the button is being pushed but I cannot figure out how to get it to show how many times to has been pushed!! Can someone please help!!


    2 -Write a program that display two buttons, OK and Cancel, in a frame. A message is display on the console to indicate which button is clicked and how many time was each button clicked and how many time were both button clicked.

     
    import javax.swing.*;
    import java.awt.event.*;
     
    public class Ok_Cancel_Button extends JFrame {
    public Ok_Cancel_Button()
    {
    	JButton Ok = new JButton("OK");
    	JButton Cancel = new JButton("Cancel");
     
    	JPanel panel = new JPanel();
    	panel.add(Ok);
    	panel.add(Cancel);
     
    	add(panel);
     
    	ListenerClass listener = new ListenerClass();
    	Ok.addActionListener(listener);
    	Cancel.addActionListener(listener);
    }
     
     
    public static void main (String[] args)
    {
    	JFrame frame = new Ok_Cancel_Button();
    	frame.setTitle ("Ok/Canel Buttons");
    	frame.setSize (500, 500);
    	frame.setLocation(400, 400);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);	
    }
    }
    class ListenerClass implements ActionListener
    {
    	public void actionPerformed (ActionEvent e) 
    	{
    	System.out.println ("The " + e.getActionCommand() + " button was clicked ");
    }
    }

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: OK_Cancel_Button

    If you need to record how many times a button was clicked, keep a variable which stores the information. You need to display which button and how many times it was clicked and you can do this using the variable that stores the number of times the button was clicked, showing it in a GUI element like a JLabel.