AWT>Menu Components>MenuItem
Menu에서 항목을 표현한다.
선택되면 이벤트를 발생시킨다.
enable()/disable() 메소드로 사용가능/불가능으로 지정가능하다.
import java.awt.*;
import java.awt.event.*;
public class TestMenuItems extends Frame implements ActionListener {
MenuBar mBar;
Menu breadMenu, toastMenu;
Menu helpMenu;
TextField tField;
public TestMenuItems() {
breadMenu = new Menu("Bread");
MenuItem tempMenuItem;
tempMenuItem = new MenuItem("White");
tempMenuItem.addActionListener(this);
breadMenu.add(tempMenuItem);
tempMenuItem = new MenuItem("Wheat");
tempMenuItem.addActionListener(this);
breadMenu.add(tempMenuItem);
tempMenuItem = new MenuItem("Rye");
tempMenuItem.addActionListener(this);
breadMenu.add(tempMenuItem);
toastMenu = new Menu("Toast");
tempMenuItem = new MenuItem("Light");
tempMenuItem.addActionListener(this);
toastMenu.add(tempMenuItem);
tempMenuItem = new MenuItem("Medium");
tempMenuItem.addActionListener(this);
toastMenu.add(tempMenuItem);
tempMenuItem = new MenuItem("Dark");
tempMenuItem.addActionListener(this);
toastMenu.add(tempMenuItem);
mBar = new MenuBar();
mBar.add(breadMenu);
mBar.add(toastMenu);
helpMenu = new Menu("Help");
helpMenu.add(new MenuItem("help"));
mBar.setHelpMenu(helpMenu);
tField = new TextField(" ", 30);
setLayout(new FlowLayout());
add(tField);
setMenuBar(mBar);
addWindowListener(new WinCloser());
setTitle("Using Menu Bars");
setBounds(100,100,300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand(); //어떤항목이 사용자에게 선택되었는지 알수있다
tField.setText("You selected : " + cmd);
}
public static void main(String[] args) {
TestMenuItems tmi = new TestMenuItems();
}
}
class WinCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}