gpt4 book ai didi

java - 按键绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-30 09:28:44 26 4
gpt4 key购买 nike

我让 JButton 通过 ActionListener 执行一些操作。在我尝试使用 Action 绑定(bind)键盘快捷键后(在 this 之后),鼠标点击按钮有效,但对键盘没有反应。

之前的代码

在面板中创建的按钮,添加了 actionListener。

private FooActionListener actionListener = new FooActionListener();

buttonLeft = new JButton("Left");
up.addActionListener(actionListener);

然后,在主类之外的 FooActionListener 类中的 actionPerformed 方法:

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == buttonLeft) { thing.move(Direction.LEFT); }
}

之后的代码

final String leftText = "Left";
final Action left = new AbstractAction() {

@Override
public void actionPerformed(ActionEvent e) {
thing.move(Direction.LEFT);
}
};

buttonLeft = new JButton(left);
buttonLeft.setText(leftText);
KeyStroke keyLeft = KeyStroke.getKeyStroke(KeyEvent.VK_A, 0);
buttonLeft.getInputMap(buttonLeft.WHEN_IN_FOCUSED_WINDOW).put(keyLeft,
"Left");
buttonLeft.getActionMap().put("Left", left );

更新:我不太确定新代码是否真的能按预期执行鼠标操作。假设对象应该通过一次单击移动 25 个像素,并且它在原始代码中确实如此。但是对于新 Action ,它似乎每次点击都会移动两次甚至三次,这表明 Action 有一些奇怪的行为。

最佳答案

按钮可能吸收了您的映射,但是,我会以稍微不同的方式来做。

因为您使用了 Action(正确),所以您的移动逻辑大部分已经集中了。

我会简单地将映射应用于主容器。

public class TestKeybindings01 {

public static void main(String[] args) {
new TestKeybindings01();
}

public TestKeybindings01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel label;
private JButton left;
private JButton right;

public TestPane() {

label = new JLabel("Make a choice");
label.setHorizontalAlignment(JLabel.CENTER);

LeftAction leftAction = new LeftAction(label);
RightAction rightAction = new RightAction(label);

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");

ActionMap am = getActionMap();
am.put("left", leftAction);
am.put("right", rightAction);

left = new JButton(leftAction);
right = new JButton(rightAction);

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(label, gbc);

gbc.gridy++;
gbc.gridwidth = 1;
add(left, gbc);
gbc.gridx++;
add(right, gbc);


}

}

public abstract class AbstractDirectionAction extends AbstractAction {

private JLabel label;

public AbstractDirectionAction(JLabel label) {
this.label = label;
}

public JLabel getLabel() {
return label;
}

public void setDirection(String text) {
getLabel().setText(text);
}

}

public class LeftAction extends AbstractDirectionAction {

public LeftAction(JLabel label) {
super(label);
putValue(NAME, "<<");
}

@Override
public void actionPerformed(ActionEvent e) {
setDirection("Left");
}

}

public class RightAction extends AbstractDirectionAction {

public RightAction(JLabel label) {
super(label);
putValue(NAME, ">>");
}

@Override
public void actionPerformed(ActionEvent e) {
setDirection("Right");
}

}

}

关于java - 按键绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com