我正在尝试获得一些基本的键盘输入功能,如箭头键。 但是,我没有尝试似乎工作,我一直在使用KeyListener接口,检查输入的JPanel得到焦点,似乎没有错误。
public class PlayField extends JPanel implements KeyListener
{
private SpelModel mijnmodel;
private boolean rechts = false;
private boolean links = false;
public PlayField(SpelModel mijnmodelArg)
{
setBackground(Color.WHITE);
mijnmodel = mijnmodelArg;
this.setFocusable(true);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); //some methods that i've taken out of the example
drawStones(g,mijnmodel.getStenen());
drawPeddle(g,mijnmodel.getBat());
drawBall(g,mijnmodel.getBall());
}
@Override
public void keyPressed(KeyEvent e){
System.out.println("Key Pressed!!!");
}
//Called when the key is released
@Override
public void keyReleased(KeyEvent e){
System.out.println("Key Released!!!");
}
//Called when a key is typed
@Override
public void keyTyped(KeyEvent e){}
}
问题可能在于我使用多个类来创建一个窗口(第一个JFrame,然后一个包含2个JPanel的JPanel,其中是“PlayField”类)并且在另一个面板中无法实现焦点? 什么是最好的行动方案?
答案 0 :(得分:6)
问题: