[求助]JMenu下的JMenu如何实现快捷键功能?送分
我在模仿MyEclipse菜单栏 file为JEmenu对象 可以用 file.setMnemonic(KeyEvent.VK_F); 实现Alt+F快捷键
但是他下面一级的New项目 怎么实现快捷键 Alt+Shift+N 弹出下级菜单,并在New后面有提示信息“Alt+Shift+N”
[code]
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
/**
* 应用程序窗口JFrame
*/
class MyJFrame extends JFrame {
private static final long serialVersionUID = 20090812;
public MyJFrame() {
// 创建窗口并设置大小
setSize(600, 500);
// 设置myFrame窗口居中
int myFrameWidth = getWidth();
int myFrameHeight = getHeight();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setLocation((screenWidth - myFrameWidth) / 2,
(screenHeight - myFrameHeight) / 2);
// 设置图标
ImageIcon icon = new ImageIcon("image/1.jpg");
setIconImage(icon.getImage());
// 设置关闭和显示
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
/**
* 菜单条JMenuBar
*/
class MyJMenuBar extends JMenuBar {
private static final long serialVersionUID = 20090812;
public MyJMenuBar() {
ImageIcon icon;
JRadioButtonMenuItem rbMenuItem;
/**
* File菜单
*/
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
// 设置字体
// file.setFont(new Font("宋体", Font.PLAIN, 15));
this.add(file);
// 设置弹出菜单大小
// JPopupMenu popupFile=file.getPopupMenu();
// popupFile.setPopupSize(300,100);
// File的JPopupMenu
JMenu file_New = new JMenu("New");
file_New.setMnemonic(KeyEvent.VK_N);
file.add(file_New);
//file_New的JPopupMenu
JMenuItem file_New_One = new JMenuItem("One");
file.add(file_New_One);
JMenuItem file_New_Two = new JMenuItem("Two");
file.add(file_New_Two);
JMenuItem file_New_Three = new JMenuItem("Three");
file.add(file_New_One);
file_New.add(file_New_One);
file_New.add(file_New_Two);
file_New.add(file_New_Three);
JMenuItem file_OpenFile = new JMenuItem("Open File...", KeyEvent.VK_O);
file.add(file_OpenFile);
// 分隔符1
file.addSeparator();
JMenuItem file_Close = new JMenuItem("Close", KeyEvent.VK_C);
file_Close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
ActionEvent.CTRL_MASK));
file.add(file_Close);
JMenuItem file_CloseAll = new JMenuItem("Close All", KeyEvent.VK_L);
file.add(file_CloseAll);
// 分隔符2
file.addSeparator();
JMenuItem file_Save = new JMenuItem("Save", KeyEvent.VK_S);
icon = new ImageIcon("image/Save.jpg");
file_Save.setIcon(icon);
file.add(file_Save);
JMenuItem file_SaveAs = new JMenuItem("Save As...", KeyEvent.VK_A);
icon = new ImageIcon("image/SaveAs.jpg");
file_SaveAs.setIcon(icon);
file.add(file_SaveAs);
JMenuItem file_SaveAll = new JMenuItem("Save All", KeyEvent.VK_E);
icon = new ImageIcon("image/SaveAll.jpg");
file_SaveAll.setIcon(icon);
file.add(file_SaveAll);
JMenuItem file_Revert = new JMenuItem("Revert ", KeyEvent.VK_T);
file.add(file_Revert);
// 分隔符3
file.addSeparator();
JMenuItem file_move = new JMenuItem("Move...", KeyEvent.VK_V);
file.add(file_move);
JMenuItem file_rename = new JMenuItem("Rename...", KeyEvent.VK_M);
file.add(file_rename);
JMenuItem file_refresh = new JMenuItem("Refresh", KeyEvent.VK_F);
file.add(file_refresh);
JMenu file_convert = new JMenu("Convert line Delimiters To ");
file_convert.setMnemonic(KeyEvent.VK_V);
file.add(file_convert);
// 分隔符4
file.addSeparator();
JCheckBoxMenuItem file_CheckBox1 = new JCheckBoxMenuItem("I love you");
file_CheckBox1.setMnemonic(KeyEvent.VK_C);
file.add(file_CheckBox1);
JCheckBoxMenuItem file_CheckBox2 = new JCheckBoxMenuItem("You love me");
file_CheckBox2.setMnemonic(KeyEvent.VK_D);
file.add(file_CheckBox2);
// 分隔符5
file.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
file.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
file.add(rbMenuItem);
// 分隔符6
file.addSeparator();
JMenuItem file_exit = new JMenuItem("Exit", KeyEvent.VK_X);
file.add(file_exit);
/**
* Edit菜单
*/
JMenu edit = new JMenu("Edit");
edit.setMnemonic(KeyEvent.VK_E);// 快捷键
JPopupMenu popupEdit = edit.getPopupMenu();
popupEdit.setPopupSize(300, 100);
add(edit);
// 鼠标监听器
MouseListener ml = new MouseAdapter() {
boolean flag=true;
MenuElement me[];
public void mouseEntered(MouseEvent e) {
JMenu menu = (JMenu) e.getComponent();
if(flag){
me = new MenuElement[2];
me[0] = (MenuElement) menu.getParent();
me[1] = menu;
}
else{
me = new MenuElement[3];
me[0] = (MenuElement) menu.getParent();
me[1] = menu;
me[2] = menu.getPopupMenu();
}
MenuSelectionManager.defaultManager().setSelectedPath(me);
}
public void mousePressed(MouseEvent e) {
flag=!flag;
mouseEntered(e);
}
};
file.addMouseListener(ml);
edit.addMouseListener(ml);
}
}
// 测试区
public class Test {
public static void main(String[] args) {
MyJMenuBar myJMenuBar = new MyJMenuBar();
MyJFrame myJFrame = new MyJFrame();
myJFrame.setJMenuBar(myJMenuBar);
}
}[/code
[ 本帖最后由 usbboy2009 于 2009-8-13 13:54 编辑 ]