3

请看下面的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuActions extends JFrame
{
    private JMenuBar jmb;
    private JMenu file;
    private JMenuItem open;

    public MenuActions()
    {
        jmb = new JMenuBar();
        file = new JMenu("File");
        open = new JMenuItem("Open");
        open.addActionListener(new MenuAction());
        open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.VK_P,ActionEvent.CTRL_MASK));

        file.add(open);
        jmb.add(file);

        this.setJMenuBar(jmb);

        getContentPane().add(new JPanel());

        this.setSize(200,200);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class MenuAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(null,"OK");

        }
    }
    public static void main(String[]args)
    {
        new MenuActions();
    }
}

在这里,当同时按下 CTRL+O+P 时,我需要触发 JMenuItem 的 EventHandler,所以它会显示 JOptionPane 说“OK”。但正如你所看到的,我的尝试给出了一个错误!当这三个键同时按下时,我该怎么做?请帮忙!

4

2 回答 2

3

看起来您使用的是错误版本的 KeyStroke.getKeyStroke() 方法 - 甚至找不到采用 3 个 int 参数的方法。不过,如果你愿意,你可以使用CTL++而 不是ALT++PCTLOP

尝试使用这个版本: http ://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String )

像这样:KeyStroke.getKeyStroke("control alt P")

在这里试试这个代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuActions extends JFrame
{
    private JMenuBar jmb;
    private JMenu file;
    private JMenuItem open;

    public MenuActions()
    {
        jmb = new JMenuBar();
        file = new JMenu("File");
        open = new JMenuItem("Open");
        open.setAction(new MenuAction("Open", null, "Click to Open an Existing File.", KeyStroke.getKeyStroke("control alt P")));
        open.setAccelerator(KeyStroke.getKeyStroke("control alt P"));

        file.add(open);
        jmb.add(file);

        this.setJMenuBar(jmb);

        getContentPane().add(new JPanel());

        this.setSize(200,200);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class MenuAction extends AbstractAction
    {
        public MenuAction(String title, ImageIcon image
                                        , String toolTipText
                                        , KeyStroke acceleratorKey)
        {
            super(title, image);
            putValue(SHORT_DESCRIPTION, toolTipText);
            putValue(SHORT_DESCRIPTION, toolTipText);
            putValue(ACCELERATOR_KEY, acceleratorKey);
        }
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(null,"OK");
        }
    }
    public static void main(String[]args)
    {
        new MenuActions();
    }
}
于 2012-05-19T15:28:12.473 回答
3

给@Pete

您可以组合任何 non_chars 加速器,但不能用于范围[a-z]&&中的键[0-9]

对于 JMenu(Item) 加速器,您可以使用

KeyEventCharacter.valueOf('char')字符[a-z]&&[0-9]

并作为第二个参数

Eventor ActionEventor InputEvent,注意每个 API 实现不同的键盘映射

可以结合 KeyStroke 但按位 | 或 & 但返回奇怪的按键

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class MenuExample extends JPanel {

    private static final long serialVersionUID = 1L;
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon questIcon = UIManager.getIcon("OptionPane.questionIcon");
    private JTextPane pane;
    private JMenuBar menuBar;

    public MenuExample() {
        menuBar = new JMenuBar();
        JMenu formatMenu = new JMenu("Justify");
        formatMenu.setMnemonic('J');
        MenuAction leftJustifyAction = new MenuAction("Left", errorIcon);
        MenuAction rightJustifyAction = new MenuAction("Right", infoIcon);
        MenuAction centerJustifyAction = new MenuAction("Center", warnIcon);
        MenuAction fullJustifyAction = new MenuAction("Full", questIcon);
        JMenuItem item;
        item = formatMenu.add(leftJustifyAction);
        item.setMnemonic('L');
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
        item = formatMenu.add(rightJustifyAction);
        item.setMnemonic('R');
        item.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK | KeyEvent.VK_N, ActionEvent.CTRL_MASK & KeyEvent.VK_B));// CTRL +N
        item = formatMenu.add(centerJustifyAction);
        item.setMnemonic('C');
        item.setAccelerator(KeyStroke.getKeyStroke(InputEvent.ALT_MASK | Character.valueOf('p'), InputEvent.ALT_MASK & Character.valueOf('o')));//ALT+F9
        item = formatMenu.add(fullJustifyAction);
        item.setMnemonic('F');
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK | Event.SHIFT_MASK));
        menuBar.add(formatMenu);
        menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

    }

    class MenuAction extends AbstractAction {

        public MenuAction(String text, Icon icon) {
            super(text, icon);
        }

        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0, "Action [" + e.getActionCommand() + "] performed!\n", null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String s[]) {
        MenuExample example = new MenuExample();
        example.pane = new JTextPane();
        example.pane.setPreferredSize(new Dimension(250, 250));
        example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
        JFrame frame = new JFrame("Menu Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(example.menuBar);
        frame.getContentPane().add(example.pane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}
于 2012-05-19T16:02:26.343 回答