为什么此KeyEvent无法正常工作? [英] Why wont this KeyEvent work?
本文介绍了为什么此KeyEvent无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Java制作一些东西,当按下F1键时会弹出一个JDialog窗口.我当前的代码:
I am making something in java that when the F1 key is hit a JDialog window to apear.My current code:
public class Keyboard implements KeyListener {
private boolean[] keys = new boolean[120];
public boolean up, down, left, right, assets;
public void tick() {
assets = keys[KeyEvent.VK_F1];
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {
}
}
在我的主类中的tick()方法下:
And in my main class under the tick() method:
keyboard.tick();
if(keyboard.assets) ac.run();
keyboard变量指的是键盘类,而ac变量指的是此类:
The keyboard variable refers to the keyboard class while the ac variable refers to this class:
public class AssetsChooser extends JDialog {
JFileChooser fc = new JFileChooser();
public void run() {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
add(fc);
System.out.println("It works.");
}
}
当我运行游戏并按F1时,没有JDialog窗口出现,控制台也没有显示该方法.
When I run my game and hit F1 no JDialog window appears nor does the Console display the method.
推荐答案
Swing中经常存在与KeyListener
相关的焦点问题.如KeyListener
教程中所述:
There are often focus issues related with KeyListener
in Swing. As noted in the KeyListener
tutorial:
查看全文