import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
/**
* The Class JFrameActionListenerTest.
*/
public class JFrameActionListenerTest extends JFrame implements ActionListener {
private JPanel content1 = new JPanel();
private JPanel content2 = new JPanel();
private JPanel content3 = new JPanel();
private JMenuItem menuItem1 = new JMenuItem("Content 1");
private JMenuItem menuItem2 = new JMenuItem("Content 2");
private JMenuItem menuItem3 = new JMenuItem("Content 3");
/**
* Create a new instance of <code>JFrameActionListenerTest</code>.
*/
public JFrameActionListenerTest() {
super("JFrameActionListenerTest");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
content1.setBackground(Color.RED);
content2.setBackground(Color.BLUE);
content3.setBackground(Color.GREEN);
final JMenuBar menuBar = new JMenuBar();
final JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
menuFile.add(menuItem1);
menuItem1.addActionListener(this);
menuFile.add(menuItem2);
menuItem2.addActionListener(this);
menuFile.add(menuItem3);
menuItem3.addActionListener(this);
setJMenuBar(menuBar);
}
/** {@inheritDoc} */
public void actionPerformed(final ActionEvent e) {
final Object source = e.getSource();
if (source == menuItem1) {
setContentPane(content1);
} else if (source == menuItem2) {
setContentPane(content2);
} else if (source == menuItem3) {
setContentPane(content3);
}
getRootPane().updateUI();
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrameActionListenerTest();
final Dimension frameSize = new Dimension(600, 400);
frame.setSize(frameSize);
final Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
final int frameX = (screenSize.width - frameSize.width) / 2;
final int frameY = (screenSize.height - frameSize.height) / 2;
frame.setLocation(frameX, frameY);
frame.setVisible(true);
}
}