import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUIMenu extends JFrame implements ActionListener
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 250;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private JLabel response;
private JMenu fileMenu;
private JMenu editMenu;
public static void main(String[] args)
{
GUIMenu frame = new GUIMenu();
frame.setVisible(true);
}
public GUIMenu( )
{
setTitle ("JMenuFrame: Testing Swing Menus");
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setResizable(false);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
createFileMenu();
createEditMenu();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
response = new JLabel("Hello, this is your menu tester");
response.setBounds(50, 50, 250, 50);
contentPane.add(response);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
String menuName;
menuName = event.getActionCommand();
if (menuName.equals("Quit"))
{
System.exit(0);
}
else
{
response.setText("Menu Item '" + menuName + "' is selected.");
Container contentPane = getContentPane();
}
}
private void createFileMenu()
{
JMenuItem item;
fileMenu = new JMenu("File");
item = new JMenuItem("New");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open...");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Save as...");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item = new JMenuItem("Quit");
item.addActionListener(this);
fileMenu.add(item);
}
private void createEditMenu()
{
JMenuItem item;
editMenu = new JMenu("Edit");
item = new JMenuItem("Cut");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
editMenu.add(item);
}
} // End of whole class