5
我被困在一些步骤,我不能添加Shorcut重点,如:CTRL + SPACE 到我的计划,我搜索一个星期,我could'd找到任何 答案。如何将快捷键添加到JTextField?
我被困在一些步骤,我不能添加Shorcut重点,如:CTRL + SPACE 到我的计划,我搜索一个星期,我could'd找到任何 答案。如何将快捷键添加到JTextField?
你会想看看Java Tutorial关键绑定的一个很好的概述。
这里有一个简单的例子:
import java.awt.event.*;
import javax.swing.*;
public class KeyBindings extends Box{
public KeyBindings(){
super(BoxLayout.Y_AXIS);
final JTextPane textArea = new JTextPane();
textArea.insertComponent(new JLabel("Text"));
add(textArea);
Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText("New Text");
}};
String keyStrokeAndKey = "control SPACE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
textArea.getInputMap().put(keyStroke, keyStrokeAndKey);
textArea.getActionMap().put(keyStrokeAndKey, action);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new KeyBindings());
frame.pack();
frame.setVisible(true);
}
}
你猜怎么着!这是工作。 感谢您的建议和感谢解决方案 这是相同的JTextField,我会查找keyBindings。 – Azad
阅读关于键绑定:http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html –