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

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();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.