以下是我的小程序游戏的精简代码:
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class Game extends Applet implements KeyListener, Runnable {
Button options = new Button("Options");
Thread thread = new Thread(this);
public void init() {
addKeyListener(this);
thread.start();
}
public void paint(Graphics graphics) {
// draw stuff
}
public void run() {
try {
while (true) {
thread.sleep(40);
repaint();
}
} catch (InterruptedException exception) {}
}
public void keyPressed(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
// pause game
add(options);
}
}
public void keyReleased(KeyEvent keyEvent) {}
public void keyTyped(KeyEvent keyEvent) {}
}
我的游戏按预期运行。但是,当用户按下时,Esc
我想暂停游戏并显示一个选项按钮。
问题是当我按下Esc
它时会按预期暂停游戏。但是它不会在屏幕上显示按钮。我试图寻找解决方案无济于事。到底发生了什么?