菜单MenuBar Menu MenuItem
调用Frame对象的setMenuBar()方法,设置菜单,参数:MenuBar对象
import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MenuBarDemo { private Frame frame; private MenuBar menuBar; private Menu menu; private Menu subMenu; private MenuItem menuItem; public MenuBarDemo() { init(); addEvents(); } public void init() { frame = new Frame("菜单测试"); frame.setBounds(300, 200, 500, 400); frame.setLayout(new FlowLayout()); // 菜单栏 menuBar = new MenuBar(); // 菜单 menu = new Menu("文件"); // 菜单项 menuItem = new MenuItem("退出"); // 菜单添加菜单项 menu.add(menuItem); // 菜单栏添加菜单 menuBar.add(menu); frame.setMenuBar(menuBar); frame.setVisible(true); } public void addEvents(){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); //菜单点击 menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } /** * @param args */ public static void main(String[] args) { new MenuBarDemo(); } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12637.html