您将需要看一下Java
Tutorial,以更好地了解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); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)