我有一个JList填充了本机复制方法无法处理的条目。我有一个方法可以按照我想要的方式有效地复制和格式化所选数据,并且我希望在使用ctrl + c时调用此方法但不幸的是JList已经列出了那些加速键,所以当我为我的副本设置它们时在我的JMenu中,除非我给予JMeny焦点,否则它不起作用。反正我是否可以让ctrl + c使用我的复制方法而不是原生的方法,而不是每次我想要复制一些东西时点击JMenu?
答案 0 :(得分:1)
首先看看How to Use Key Bindings和How to Use Actions
首先创建一个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;
}
}
现在,添加一个键绑定到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;
}
}
}