0

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

4

1 回答 1

1

首先看看如何使用键绑定如何使用操作

首先创建一个Action描述它做什么以及它是如何做的。在这种情况下,您可能需要提供JList要从中复制的内容(我会interface提供某种简单的“复制”方法,但这只是我)

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

}

现在,将键绑定添加到 a 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;
    }

}

现在,您也可以使用Actionwith 按钮...

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

    }

}
于 2015-07-15T06:25:49.813 回答