菜单代码示例: import java.awt.*; import java.awt.event.*;
class MyMenuDemo2 { private Frame f; private MenuBar mb; private Menu m; private MenuItem mz; private MenuItem mz2; private MenuItem mz3; private Menu mi; private MenuItem tc;
MyMenuDemo2() { init(); }
private void init() { f=new Frame("my window"); mb=new MenuBar(); m=new Menu("文件"); mz=new MenuItem("子条目"); mz2=new MenuItem("子条目2"); mz3=new MenuItem("子条目3"); mi=new Menu("子菜单"); tc=new MenuItem("退出");
f.setBounds(200,300,400,500); f.setLayout(new FlowLayout());
f.setMenuBar(mb); mb.add(m);
m.add(tc); m.add(mi);
mi.add(mz2); mi.add(mz3);
MyEvent(); f.setVisible(true); }
private void MyEvent() { tc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
} public static void main(String[] args) { new MyMenuDemo2(); } |