如何设置在一个事件中处理所有数字笔画的键绑定?
How to setup key bindings that processes all number strokes in one event?
我已经注册了按键绑定,我想对所有数字按键做出反应。我可以为每个键 (0-9) 注册不同的事件,但这有点愚蠢。那么有没有可能一次搞定呢?
这是我的代码,它只对小键盘上的键 0 有反应:
private void setKeyBindings() {
AbstractAction aa = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Here");
}
};
this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
this.editButton.getActionMap().put("0", aa);
}
谢谢
你知道的问题是如何识别动作响应的击键(或为什么调用动作)
您可以创建一个特殊的动作,而不是使用动作的单个实例,您可以为它植入完成其工作所需的信息
public class NumberAction extends AbstractAction {
private int number;
public NumberAction(int number) {
this.number = number
}
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Here");
}
}
然后,您可以根据需要创建它们....
private void setKeyBindings() {
this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
this.editButton.getActionMap().put("0", new NumberAction(0));
//Other numbers...
}
So is it possible to handle it all in one event?
您可以创建一个事件侦听器以供所有绑定使用:
可能是这样的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CalculatorPanel extends JPanel
{
private JTextField display;
public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
// display.setCaretPosition( display.getDocument().getLength() );
System.out.println(e.getActionCommand());
display.replaceSelection(e.getActionCommand());
}
};
setLayout( new BorderLayout() );
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(30, 30) );
buttonPanel.add( button );
InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(text), text);
inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
buttonPanel.getActionMap().put(text, numberAction);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
我已经注册了按键绑定,我想对所有数字按键做出反应。我可以为每个键 (0-9) 注册不同的事件,但这有点愚蠢。那么有没有可能一次搞定呢?
这是我的代码,它只对小键盘上的键 0 有反应:
private void setKeyBindings() {
AbstractAction aa = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Here");
}
};
this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
this.editButton.getActionMap().put("0", aa);
}
谢谢
你知道的问题是如何识别动作响应的击键(或为什么调用动作)
您可以创建一个特殊的动作,而不是使用动作的单个实例,您可以为它植入完成其工作所需的信息
public class NumberAction extends AbstractAction {
private int number;
public NumberAction(int number) {
this.number = number
}
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Here");
}
}
然后,您可以根据需要创建它们....
private void setKeyBindings() {
this.editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "0");
this.editButton.getActionMap().put("0", new NumberAction(0));
//Other numbers...
}
So is it possible to handle it all in one event?
您可以创建一个事件侦听器以供所有绑定使用:
可能是这样的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CalculatorPanel extends JPanel
{
private JTextField display;
public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
// display.setCaretPosition( display.getDocument().getLength() );
System.out.println(e.getActionCommand());
display.replaceSelection(e.getActionCommand());
}
};
setLayout( new BorderLayout() );
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(30, 30) );
buttonPanel.add( button );
InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(text), text);
inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
buttonPanel.getActionMap().put(text, numberAction);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}