package itheima005; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; public class SimplemenuDemo { Frame frame=new Frame("测试菜单条组件"); MenuBar menuBar=new MenuBar(); Menu file=new Menu("文件"); Menu edite=new Menu("编辑"); Menu format =new Menu("格式"); MenuItem auto=new MenuItem("自动换行"); MenuItem copy=new MenuItem("复制"); MenuItem paste=new MenuItem("粘贴"); MenuItem comment=new MenuItem("注释",new MenuShortcut(KeyEvent.VK_Q,true)); MenuItem uncomment=new MenuItem("取消注释"); TextArea ta=new TextArea(6,30); public void init(){ comment.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); ta.append("你点击了"+actionCommand+"\n"); } }); //组装视图 format.add(comment); format.add(uncomment); edite.add(auto); edite.add(copy); edite.add(paste); edite.add(format); menuBar.add(file); menuBar.add(edite); frame.setMenuBar(menuBar); frame.add(ta); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new SimplemenuDemo().init(); } }