Results 1 to 3 of 3
  1. #1
    Snatch is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Ok_Calncel_Button

    This is what I have to do, and the program is what I done I am having problems with the red items!! Please help Thank You!

    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.

    Java Code:
    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(500, 500);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);	
    }
    
    [COLOR="Red"]class ListenerClass implements ActionListener
    {
    	public void actionPerformed (ActionEvents e); 
    	
    	System.out.println ("The " + getActionCommand + " button was clicked " + NumClick + "times.");
    }[/COLOR]
    
    }

  2. #2
    Snatch is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok I got it to work now I have to figure out how to let me know how many times the buttons was hit!!! Here is what I got!! Can someone help!

    Java Code:
    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 ");
    }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

    Default

    Perhaps you want to give your ListenerClass some int fields to act as counters and then use the ActionCommand to determine which counter to increment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •