gpt4 book ai didi

java - 防止用户在 Eclipse 中按住按键

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:38 25 4
gpt4 key购买 nike

我正在创建一个田径游戏,我需要玩家点击按钮来填充仪表。当玩家松开按键时,仪表就会下降。不幸的是,当我按住按钮时,仪表就填满了。

代码:

public class TapGame extends JPanel implements ActionListener, KeyListener{

Timer tm = new Timer(5,this);
int barHeight = 0, bar = 0;
boolean canPress = true;

public TapGame(){
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(0, 355, bar, 10);
g.setColor(Color.WHITE);
g.drawRect(0, 355, 100, 10);

}


public void actionPerformed(ActionEvent e){
bar+= barHeight;
bar -= 2;
if (bar < 0) bar = 0;
if (bar > 98) bar = 98;
repaint();

}

public void keyPressed(KeyEvent e){
int c = e.getKeyCode();

if (c == KeyEvent.VK_A){
if (canPress) {
barHeight = 4;
canPress = false;
}
else barHeight=0;

}
}

public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
canPress = true;
barHeight =0;
}

public static void main(String[] args){
TapGame t = new TapGame();

JFrame jf = new JFrame("Tap Mini Game Test");

jf.setSize(600,400);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.add(t);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

最佳答案

关于java - 防止用户在 Eclipse 中按住按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068470/

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