有没有办法覆盖本机加速键?

Is there a way to override native accelerator keys?

我有一个 JList,其中填充了本机复制方法无法处理的条目。我有一种方法可以按照我想要的方式有效地复制和格式化所选数据,我希望在使用 ctrl + c 时调用此方法,但不幸的是 JList 已经列出了这些加速键,所以当我为我的副本设置它们时在我的 JMenu 中运行它不起作用,除非我给予 JMeny 焦点。无论如何我可以让 ctrl + c 使用我的复制方法而不是本地方法,而不是每次我想复制东西时都必须单击 JMenu?

先看看 How to Use Key Bindings and How to Use Actions

首先创建一个 Action 来描述它的作用和作用。在这种情况下,您可能需要提供要从中复制的 JList(我有某种 interface,它提供了某种简单的 "copy" 方法,但是这就是我)

public class CopyAction extends AbstractAction {

    private JList list;

    public CopyAction(JList list) {
        putValue(NAME, "Copy");
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK));
        putValue(MNEMONIC_KEY, KeyEvent.VK_C);
        putValue(SELECTED_KEY, "Copy stuff");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("All your copies belong to us");
    }

    public JList getList() {
        return list;
    }

}

现在,将键绑定添加到 JList,以及您的 Action...

public class TestPane extends JPanel {

    private JList listOfStuff;

    public TestPane() {
        setLayout(new BorderLayout());
        listOfStuff = new JList();
        add(new JScrollPane(listOfStuff));

        CopyAction copyAction = new CopyAction(listOfStuff);
        listOfStuff.getActionMap().put("copy", copyAction);
    }

    public JList getListOfStuff() {
        return listOfStuff;
    }

}

现在,您也可以将 Action 与按钮一起使用...

TestPane tp = new TestPane();
CopyAction copyAction = new CopyAction(tp.getListOfStuff());

JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Edit");
mb.add(menu);
menu.add(copyAction);

JFrame frame = new JFrame("Testing");
frame.setJMenuBar(mb);

还有一个完全可运行的示例....

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public class CopyAction extends AbstractAction {

        private JList list;

        public CopyAction(JList list) {
            putValue(NAME, "Copy");
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK));
            putValue(MNEMONIC_KEY, KeyEvent.VK_C);
            putValue(SELECTED_KEY, "Copy stuff");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("All your copies belong to us");
        }

        public JList getList() {
            return list;
        }

    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TestPane tp = new TestPane();
                CopyAction copyAction = new CopyAction(tp.getListOfStuff());

                JMenuBar mb = new JMenuBar();
                JMenu menu = new JMenu("Edit");
                mb.add(menu);
                menu.add(copyAction);

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(mb);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JList listOfStuff;

        public TestPane() {
            setLayout(new BorderLayout());
            listOfStuff = new JList();
            add(new JScrollPane(listOfStuff));

            CopyAction copyAction = new CopyAction(listOfStuff);
            listOfStuff.getActionMap().put("copy", copyAction);
        }

        public JList getListOfStuff() {
            return listOfStuff;
        }

    }

}