//JMenu 继承自JMenuItem,它们都在javax.swing包中,都实现了Accessible, MenuElement 接口
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.MenuShortcut;
public class JMenuBarDemo
{
public static void main (String[] args)
{
//try{javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
JPanel panel = new JPanel ();
panel.setBackground (Color.white);
panel.setPreferredSize (new Dimension (600,400));
//JMenu 有箭头(除了顶层的),可以添加JMenu和JMenuItem;
//JMenuItem 无箭头,不可添加其他菜单项
JMenu file, edit, search, item, view, format, macro, advance, windows, help;
file = new JMenu ("File");
edit = new JMenu ("Edit");
search = new JMenu ("Search", true);
item = new JMenu ("Item");
view = new JMenu ("View");
format = new JMenu ("Format");
macro = new JMenu ("Macro");
advance = new JMenu ("Advance");
windows = new JMenu ("Windows");
help = new JMenu ("Help");
JMenuItem xin, open, close, save, saveAs;
xin = new JMenuItem ("New", 'n');//热键为'n'
open = new JMenuItem ("Open");
close = new JMenuItem ("Close");
save = new JMenuItem ("Save");
saveAs = new JMenuItem ("Save As");
JMenu del = new JMenu("Del");
del.add ("Del All");
JMenuItem delLine = new JMenuItem ("Del Line"); //这行加下行与上行等价
del.add (delLine);
delLine.setEnabled (false);
edit.add (del);
file.add (xin);
file.add (open);
file.add (close);
file.addSeparator ();//加一条分隔线
file.add (save);
file.add (saveAs);
JMenuBar bar = new JMenuBar ();
bar.add (file);
bar.add (edit);
bar.add (search);
bar.add (item);
bar.add (view);
bar.add (format);
bar.add (macro);
bar.add (advance);
bar.add (windows);
bar.add (help);
JFrame frame = new JFrame ("JMenuBarDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.setJMenuBar (bar);
frame.pack();
frame.show();
}
}
评论