Creating Buttons in java
Hi,
I am new to java.. i have created two butons and the objective of first button is to print a string( Say "Hello world") till the next button is pressed.
so Button1 : prints "Hello World"
Button 2: Exit.
Iam facing problem in the 2 nd button, since it is not allowing me to exit.
can some body help me in solving this problem.
Thank you
anand.
Are you sure that you've added an action listener to both buttons? For instance
[code]
public class MyButtons extends ... implements ActionListener
...
JButton button1 = new JButton("Hello World");
button1.addActionListener(this);
button1.setActionCommand("hello world");
// add the button to your layout
JButton button2 = new JButton("Exit");
button2.addActionListener(this);
button2.setActionCommand("exit");
// add the button to your layout
...
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equals("hello world")
{
System.out.println("Hello world");
} else
{
System.exit(0);
}
}