关闭

菜单栏和右键弹出框

302人阅读 评论(0) 收藏 举报
class MenuFrame extends JFrame {
 public MenuFrame() {
  this.setTitle("Menu Test");
  this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  JMenu fileMenu = new JMenu("File");
  fileMenu.add(new TestAction("New"));
  JMenuItem openItem = fileMenu.add(new TestAction("Open"));
  openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl o"));
  fileMenu.addSeparator();
  saveAction = new TestAction("Save");
  JMenuItem saveItem = fileMenu.add(saveAction);
  saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl s"));
  saveAsAction = new TestAction("Save As");
  fileMenu.add(saveAsAction);
  fileMenu.addSeparator();
  fileMenu.add(new AbstractAction("Exit") {
   public void actionPerformed(ActionEvent event) {
    System.exit(0);
   }
  });
  readonlyItem = new JCheckBoxMenuItem("Read-Only");
  readonlyItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
    boolean saveOk = !readonlyItem.isSelected();
    saveAction.setEnabled(saveOk);
    saveAsAction.setEnabled(saveOk);
   }
  });
  ButtonGroup group = new ButtonGroup();
  JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
  insertItem.setSelected(true);
  JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");
  group.add(insertItem);
  group.add(overtypeItem);
  Action cutAction = new TestAction("Cut");//copyAction,pasteAction一样
  cutAction.putValue(Action.SMALL_ICON, new ImageIcon("c:/picture/1.gif"));
  JMenu editMenu = new JMenu("Edit");
  editMenu.add(cutAction);//copyAction,pasteAction
  JMenu optionMenu = new JMenu("Options");
  optionMenu.add(readonlyItem);
  optionMenu.addSeparator();
  optionMenu.add(insertItem);//overtypeItem
  editMenu.addSeparator();
  editMenu.add(optionMenu);
  JMenu helpMenu = new JMenu("Help");
  helpMenu.setMnemonic('H');
  JMenuItem indexItem = new JMenuItem("Index");
  indexItem.setMnemonic('I');
  helpMenu.add(indexItem);
  Action aboutAction = new TestAction("About");
  aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
  helpMenu.add(aboutAction);
  JMenuBar menuBar = new JMenuBar();
  this.setJMenuBar(menuBar);
  menuBar.add(fileMenu);//editMenu,helpMenu
  //右键弹出框
  popup = new JPopupMenu();
  popup.add(cutAction);//copyAction,pasteAction
  JPanel panel = new JPanel();
  panel.setComponentPopupMenu(popup);
  this.add(panel);
  panel.addMouseListener(new MouseAdapter() {
  });
 }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:100891次
    • 积分:1751
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:0篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论