gpt4 book ai didi

java - 运行一段时间后,KeyInput 将不起作用(JCreator、Applet)

转载 作者:行者123 更新时间:2023-12-02 05:47:53 33 4
gpt4 key购买 nike

据说只要你按下一个键,它就应该访问代码,但事实并非如此,我或我的老师也不知道为什么。代码如下:

class KeyInput implements KeyListener{
Actor player;
Graphics gBuffer;

public KeyInput(Actor player, Graphics gBuffer){
JTextField typingArea = new JTextField(20);
typingArea.addKeyListener(this);
this.player = player;
this.gBuffer = gBuffer;
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
player.modActor(1);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_RIGHT) {
player.modActor(2);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_UP) {
player.modActor(3);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_DOWN) {
player.modActor(4);
gBuffer.drawString("ENTER",150,100);
}
}

}

这是节目期间播放的时间:

   public void paint(Graphics g){ //main method to be called to play(g);
do{
drawBackground(gBuffer);
gBuffer.setFont(new Font("Calibri",Font.BOLD,20));
gBuffer.setColor(Color.CYAN);
gBuffer.drawString("Score: " + (int)score,18,20);
gBuffer.drawString("Score: " + (int)score,22,20);
gBuffer.drawString("Score: " + (int)score,20,22);
gBuffer.drawString("Score: " + (int)score,20,18);
gBuffer.setColor(Color.BLACK);
gBuffer.drawString("Score: " + (int)score,20,20);
play(gBuffer);
g.drawImage(virtualMem,0,0,this);
if(lose==false){
gBuffer.setFont(new Font("Calibri",Font.BOLD,50));
gBuffer.setColor(Color.CYAN);
gBuffer.drawString("GAME OVER",42,200);
gBuffer.drawString("GAME OVER",38,200);
gBuffer.drawString("GAME OVER",40,202);
gBuffer.drawString("GAME OVER",40,198);
gBuffer.setColor(Color.BLACK);
gBuffer.drawString("GAME OVER",40,200);
gBuffer.drawString("Final Score: " + (int)score,20,260);
}
}while(lose);

}

关键输入需要移动我拥有的 Actor ,但它根本不移动,是的,我每次都在小程序内单击。知道发生了什么吗?

编辑:我现在已经放弃了 keyListener 并尝试了 keyBindings,但我对它们很陌生,可能有问题,请帮助我......

public void init() {
panel = new JPanel();
this.setSize(400,700);
this.setFocusTraversalKeysEnabled(false);
this.addKeyListener(this);
player = new Actor(180,500);
virtualMem = createImage(400,800);
gBuffer = virtualMem.getGraphics();

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
panel.getActionMap().put("up", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
panel.getActionMap().put("down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
panel.getActionMap().put("left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
panel.getActionMap().put("right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});

}

暂时还是一样(玩)。当我单击某些内容时,它也不会访问这些方法。

最佳答案

我担心,paint() 内的无限循环可能会干扰事件管理(请记住,有一个线程可以完成这两件事)。为什么不在每个重要事件(例如,按键)上触发 repaint() 呢?

关于java - 运行一段时间后,KeyInput 将不起作用(JCreator、Applet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23875063/

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