在java中的关键pressed事件 [英] KeyPressed event in java
本文介绍了在java中的关键pressed事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚创建了一个java井字棋游戏,我想弄清楚如何运行的方法,一旦进入关键是有一定条件的例子是在以下pssed $ P $ ...
如果(/ *条件得到满足* /){
//的KeyListener
}
解决方案
根据您要捕获ENTER键,你可以使用的ActionListener
(上这样的组件,如文本组件或按钮)或附加键绑定你组件
公共类MyPanel继承JPanel { 公共MyPanel(){ IM的InputMap = getInputMap中(WHEN_FOCUSED);
ActionMap中上午= getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),的OnEnter); am.put(的OnEnter,新AbstractAction(){
@覆盖
公共无效的actionPerformed(ActionEvent的五){
//输入pressed
}
}); }}
这将依赖于该组件被聚焦。
I have just created a java tic-tac-toe game i would like to figure out how to run a method once the enter key is pressed during a certain condition an example is below...
if(/*condition is met*/){
//keyListener
}
解决方案
Depending on where you want to trap the "enter" key, you could use an ActionListener
(on such components such as text components or buttons) or attach a key binding to you component
public class MyPanel extends JPanel {
public MyPanel() {
InputMap im = getInputMap(WHEN_FOCUSED);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "onEnter");
am.put("onEnter", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// Enter pressed
}
});
}
}
This will rely on the component being focused.
这篇关于在java中的关键pressed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文