【发布时间】:2013-08-16 23:55:12
【问题描述】:
这是我的 JButton,“我可以显示完整代码”,
JButton cor =new JButton();
cor.setText("Coor");
mainframe.add(cor);
window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);
我需要帮助,如何使用键盘激活此按钮,例如按“CTRL + A”
我红了一些网站,发现我必须使用“Key Bindings”,但仍然可以知道怎么做。
请不要说这个话题是重复的,因为之前的话题对我没有帮助。
cor.getInputMap().put(KeyStroke.getKeyStroke("F2"),"act");
Action act = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int xC = (int) b.getX();
int yC = (int) b.getY();
textArea.replaceSelection("X-Coordinates:" + xC + " Y-Coordinates: " + yC + "\n");
}
};
【问题讨论】:
-
没有人想看到完整的代码,但考虑在您尝试使用键绑定的地方显示您的代码。这是让我们知道您可能做错了什么的最佳方式。最好发sscce。 re
Please don't say that this topic is duplicated, because previous topics doesn't help me.考虑避免对我们可以给出的答案做出规定。毕竟我们都是志愿者;你不付钱给我们。而是告诉我们键绑定到底是什么让您感到困惑,并在您的代码中展示这一点。 -
我使用了这个代码:cor.getInputMap().put(KeyStroke.getKeyStroke("F2"), "none");
-
在上面的问题中显示,而不是在 cmets 中。还展示了如何使用 ActionMap。解决方案当然是正确使用 Key Bindings。没有人愿意为您重新编写教程,因此要获得具体帮助,您需要显示更多代码(作为对您问题的编辑)。
-
已编辑主要问题
标签: java swing keyboard key jbutton