正文

JMenuBar用法2005-09-17 01:01:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/4849.html

分享到:

//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();
 }
}

阅读(6385) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册