当前位置: 代码迷 >> J2SE >> 猎奇怪!好郁闷啊!JMenu的下拉菜单弹不出来
  详细解决方案

猎奇怪!好郁闷啊!JMenu的下拉菜单弹不出来

热度:213   发布时间:2016-04-24 17:35:29.0
好奇怪!好郁闷啊!JMenu的下拉菜单弹不出来
各位同仁,一个奇快的问题发生在如下程序中,现请高人指点:
问题是:file菜单可以弹出下拉菜单,但是无法点击下拉菜单中的选项;edit菜单弹不出下拉菜单。本程序编辑、运行无误!
/**
  *  
  */

/**
  *   @author   yongjie66@126.com
  *
  */
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   javax.swing.event.*;
public   class   JMenuTest     {

/**
  *   定义控件
  */
JFrame   frm=null;
JTextArea   txta=null;
JMenu   menuFile=null,menuEdit=null,openmenu=null;
JMenuItem   open=null,close=null,exit=null,save=null,newf=null,openOld=null,openNew=null,copy=null,paste=null,cut=null;

public   JMenuTest()   {
//   TODO   Auto-generated   constructor   stub
/*
  *   构造函数,初始化
  */
frm=new   JFrame( "JMenu   Test ");
Container   cp=frm.getContentPane();
JMenuBar     menuBar=new   JMenuBar();
frm.setJMenuBar(menuBar);

menuFile=new   JMenu( "file ");
menuEdit=new   JMenu( "Edit ");

menuBar.add(menuFile);
menuBar.add(menuEdit);

open=new   JMenuItem( "open   File ", 'O ');//设置快捷键为 'O '
//open.setMnemonic( 'O ');//设置快捷键为 'O '方法二
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.ALT_MASK));//设置快捷键‘O’,屏蔽ALT键
open.setIcon(new   ImageIcon( "2.gif "));//添加图标
newf=new   JMenuItem( "new   File ");
newf.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.ALT_MASK));
newf.setIcon(new   ImageIcon( "4.gif "));
save   =new   JMenuItem( "save   File ");
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.ALT_MASK));
save.setIcon(new   ImageIcon( "6.gif "));
close   =new   JMenuItem( "close ");
close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.ALT_MASK));
exit=new   JMenuItem( "exit ");
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.ALT_MASK));
menuFile.add(open);
menuFile.add(newf);
menuFile.add(save);
menuFile.add(close);
menuFile.addSeparator();
menuFile.add(exit);
openmenu=new   JMenu();
openOld   =new   JMenuItem( "openOld ");
openNew   =new   JMenuItem( "openNew ");
openmenu.add(openOld);
openmenu.add(openNew);
open.add(openmenu);

menuEdit=new   JMenu( "Edit ");
copy=new   JMenuItem( "copy ");
paste=new   JMenuItem( "paste ");
cut=new JMenuItem( "cut ");
menuEdit.add(copy);
menuEdit.add(paste);
menuEdit.add(cut);

/*
  *   注册菜单的事件
  */
open.addActionListener(new   ItemActionLis());
close.addActionListener(new   ItemActionLis());
exit.addActionListener(new   ItemActionLis());
save.addActionListener(new   ItemActionLis());
newf.addActionListener(new   ItemActionLis());
copy.addActionListener(new   ItemActionLis());
openOld.addActionListener(new   ItemActionLis());
openNew.addActionListener(new   ItemActionLis());
paste.addActionListener(new   ItemActionLis());
cut.addActionListener(new   ItemActionLis());
txta=new   JTextArea();
cp.add(txta);

frm.setBounds(300,300,500,400);