0

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

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Pogo
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();
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.