954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Action listeners

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

teritori
Newbie Poster
11 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

Im quite sure there is no difference between an applet and an aplication.

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame implements ActionListener {

    Button button = new Button("Push me!");

    public MyFrame() {

        super("MyFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 100);
        button.addActionListener(this);
	getContentPane().setLayout(new BorderLayout());
	getContentPane().add("Center", button);
        setVisible(true);
    }

    public final void actionPerformed(ActionEvent e) {
    
        if (e.getActionCommand().equals(button.getActionCommand())) {
            JOptionPane.showMessageDialog(this, "Hey, you pushed me!", "Button says: ", JOptionPane.PLAIN_MESSAGE);
        }
    }

    public static void main(String[] args) {
    
        MyFrame mf = new MyFrame();
    }
}
Pogo
Newbie Poster
4 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You