老实说,我不确定标题是否完全适合这个问题,但无论如何它都在这里。我在java中制作了一个简单的游戏,其中外星人的宇宙飞船从屏幕顶部掉下来,如果你不杀死它们并且它们到达屏幕的底部,那么空间站会受到伤害。但是每当空间站被摧毁时,应该告诉玩家他们已经死亡的消息框不会停止出现,它会一直反复出现。在控制台中,我收到一条不会停止变大的错误消息!这是我对空间站健康的代码:
public class SpaceStation extends Entity {
public static int stationHealth = 100;
public SpaceStation(int x, int y) {
super(x, y);
}
public void update() {
}
public void draw(Graphics2D g2d) {
g2d.drawImage(ImageLocator.getSpaceStation(), 0, 595, null);
// HEALTH BAR
g2d.fillRect(5, 25, stationHealth, 10);
if(stationHealth <= 0) {
try{
JOptionPane.showMessageDialog(null, "You died on level "
+ GameFrame.level + ". Better luck next time!");
GameFrame.mainTimer.stop();
System.exit(0);
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(0);
}
}
}
public Rectangle getBounds() {
return new Rectangle(x, y, 600, 200);
}
}
显然,错误所在的行是JOptionPane.showMessageDialog(null,“你死在了关卡” 这是错误消息:
at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.InternalError: The current process has used all of its system allowance of handles for Window Manager objects.
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.InternalError: The current process has used all of its system allowance of handles for Window Manager objects.
感谢您的时间。
答案 0 :(得分:8)
不要在paint方法中打开对话框!或者可能更具域特性,不要在可能从另一个类的paint方法调用的方法中执行此操作,如draw(Graphics2D)
所示。
我只能假设您的代码有一个调用repaint()
的计时器,这反过来可能会调用paint(Graphics)
或paintComponent(Graphics)
。但是当JRE知道某个组件可能有一个元素出现在顶部时,这也是由JRE本身完成的,就像JOptionPane
一样。因此'无限循环'。