This is just a test program..
What i want to do is that when the user clicks Exit in the menu bar, a string or a label will appear in panel 2...but it doesn't work...what is wrong with my code?
Code:import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
public class test2 extends JFrame{
private static final int WIDTH=300;
private static final int HEIGHT=200;
private JPanel p1=new JPanel();
private JPanel p2=new JPanel();
public test2(){
super();
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JLabel l=new JLabel("Label 1");
p1.add(l);
JMenuItem exit=new JMenuItem("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//System.exit(0);
JLabel l2=new JLabel("Exit clicked");
p2.add(l2);
}
});
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
JMenuBar bar=new JMenuBar();
bar.add(exit);
setJMenuBar(bar);
}
public static void main(String[] args){
test2 t=new test2();
t.setVisible(true);
}
}