如何将ShortCut键添加到JTextField? [英] How To Add ShortCut Keys to JTextField?
本文介绍了如何将ShortCut键添加到JTextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困在一些步骤中,我无法添加一个Shorcut键,如: CTRL + SPACE 到我的程序,我正在搜索一周,我可以找到任何
答案。
I am Stuck in some Step that I Can't Add a Shorcut Key like: CTRL+SPACE to my program , I am Searching for one week and I could'd find any answers .
推荐答案
你想看看Java教程,可以很好地概述关键绑定。
You'll want to look at the Java Tutorial for a good overview of Key Bindings.
这是一个简单的例子:
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);
}
}
这篇关于如何将ShortCut键添加到JTextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文