[Java] Displaying A Message Box When A Button Is Clicked

Junior Spellweaver
Joined
Sep 1, 2008
Messages
130
Reaction score
0
HI there.

I've watched a video on Java Tutorials, and they are explaining how to display a message box when a button is clicked. However, I've copyed the code and complied and Ran Application, but when I click on the Button no Message Box appears. Heres the source just in case you guys spot something that I haven't done

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


public class GUILayout2 extends JFrame implements ActionListener
{
	private static GUILayout frame;
	private static JButton button;
	private static JButton button1;;

	public static void main( String[] args)
	{
		frame = new GUILayout();

		frame.setTitle("Hello World - My First GUI!");
		frame.setSize(400, 400);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public GUILayout2()
	{
		setLayout(new GridLayout(3, 2));


			JButton button = new JButton("Press Me!");
			JButton button1 = new JButton("Press Me!");
			JButton button2 = new JButton("Press Me!");
			JButton button3 = new JButton("Press Me!");
			JButton button4 = new JButton("Press Me!");

			add(button);
			add(button1);
			add(button2);
			add(button3);
			add(button4);

			button.addActionListener(this);

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button)
			JOptionPane.showMessageDialog(frame, "You pressed button" );

	}
}

Thanks'
YeImANutter
 
I think it has to do with
Code:
button.addActionListener(this);

and it looks like nothing will ever call
the actionPerformed() void..
 
Back