Message boxes will not be displayed

I'm honestly not sure that the name is fully consistent with this issue, but here it is all the same. I am making a simple java game where alien spaceships fall from the top of the screen, and if you do not kill them and they get to the bottom of the screen, the space station takes damage. But whenever the space station is destroyed, the message window in which it should inform the player that they are dead does not stop appearing, it just keeps appearing again and again. And in the console, I get an error message that will not stop increasing! This is the code I have for the health of the space station:

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);
}

}

Apparently, the line where the error is included is JOptionPane.showMessageDialog (null, "You died at level", Here is the error message:

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.

.

+5
1

! , , , , , draw(Graphics2D).

, , repaint(), , , paint(Graphics) paintComponent(Graphics). JRE , , , ,.. a JOptionPane. , " ".

+8

All Articles