Action listeners

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 11
Reputation: teritori is an unknown quantity at this point 
Solved Threads: 0
teritori teritori is offline Offline
Newbie Poster

Action listeners

 
0
  #1
Jun 25th, 2004
hi everyone.Can anione please tell me how can i create an action listener on a button in a java application.I know how to create one on an applet but somehow cant seem to do like wise on an application.Please help!.. :o
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: Pogo is an unknown quantity at this point 
Solved Threads: 0
Pogo Pogo is offline Offline
Newbie Poster

Re: Action listeners

 
0
  #2
Jul 1st, 2004
Im quite sure there is no difference between an applet and an aplication.

  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4.  
  5. public class MyFrame extends JFrame implements ActionListener {
  6.  
  7. Button button = new Button("Push me!");
  8.  
  9. public MyFrame() {
  10.  
  11. super("MyFrame");
  12. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. setSize(200, 100);
  14. button.addActionListener(this);
  15. getContentPane().setLayout(new BorderLayout());
  16. getContentPane().add("Center", button);
  17. setVisible(true);
  18. }
  19.  
  20. public final void actionPerformed(ActionEvent e) {
  21.  
  22. if (e.getActionCommand().equals(button.getActionCommand())) {
  23. JOptionPane.showMessageDialog(this, "Hey, you pushed me!", "Button says: ", JOptionPane.PLAIN_MESSAGE);
  24. }
  25. }
  26.  
  27. public static void main(String[] args) {
  28.  
  29. MyFrame mf = new MyFrame();
  30. }
  31. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8946 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC