| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2465 人关注过本帖
标题:[求助]JMenu下的JMenu如何实现快捷键功能?送分
只看楼主 加入收藏
usbboy2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:183
注 册:2009-7-31
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:3 
[求助]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 编辑 ]
搜索更多相关主题的帖子: 如何实现快捷键 
2009-08-13 12:40
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:35 
eclipse不是用swing做的,是用swt做的吧

[url=http://shop63425653./]/cvbnm/a6/1d/f4/7dd1720119cf3b1dcfb570b467b24051.jpg" border="0" />[/url]
2009-08-13 16:35
usbboy2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:183
注 册:2009-7-31
收藏
得分:0 
  大大们来帮帮忙
偶是来尝试模仿 顺便学习swing
菜单项目有2个两个问题困扰

1.JMenu下JMenu好像不能设快捷键??? JMenuBar下JMenu可以用setMnemonic实现
JMenu又不能用setAccelerator 郁闷!!!

2.JMenuItem的3键快捷键怎么实现 如"Ctrl+Alt+O" 打开一个Open窗口
网上都是2键的实现 ,3键的如何搞???

2009-08-13 16:56
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
LZ不用找了,我曾经也找过,未果

java图像界面玩玩熟悉就可以了,不用搞的那么深

[url=http://shop63425653./]/cvbnm/a6/1d/f4/7dd1720119cf3b1dcfb570b467b24051.jpg" border="0" />[/url]
2009-08-14 11:23
快速回复:[求助]JMenu下的JMenu如何实现快捷键功能?送分
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023461 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved