Results 1 to 3 of 3
Thread: Ok_Calncel_Button
- 04-04-2010, 10:14 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 6
- Rep Power
- 0
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] }
- 04-04-2010, 10:33 PM #2
Member
- Join Date
- Apr 2010
- Posts
- 6
- Rep Power
- 0
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 "); } }
-
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.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks