English 中文(简体)
JMenuItem:如何建立有3个钥匙的加速器?
原标题:JMenuItem: how to set an Accelerators with 3 keys?

请参看以下法典:

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();
    }
}

In here, I need to fire the EventHandler of the JMenuItem when CTRL+O+P is pressed together, so it will display JOptionPane saying "OK". But as you can see, my attempt is giving an error! How can I do this when three of these keys are pressed together? Please help!

最佳回答

它像你那样,采用错误的基斯特克办法,甚至可以发现有3个参数。 但如果您愿意使用CTL+ALT+而不是 CTL++

Try using this version: http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String)

如:KeyStroke.getKeyStroke(“控制带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();
    }
}
问题回答

页: 1

您可以将任何非“加速器”合并起来,但对于范围<代码>[a-z] &&<代码>[0-9]中的钥匙是不可能的。

JMenu(Item)

KeyEvent or Character. ValueOf (char ) for natures [a-z]&[0-9]

页: 1

页: 1

可将KutStroke与Bitwise ́s &合并起来,但返回主要Strokes

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!
", 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);
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签