1
if("Back".equals(e.getActionCommand()))
{
    escape.setVisible(false);
    paused = false;
    running = !running;
    Graphics g = start.getGraphics();
    g.dispose();
    Component glassPane = start.getGlassPane();
    glassPane.setVisible(!glassPane.isVisible());
    Main.main(null);
    Main.running = true;
    start.dispose();
}

This is my code for disabling my jframe, loading a new one, and disposing it. Whenever I do this, when I return to the frame I get strange graphical errors. One of the graphical errors!

How do I close the frame completly?

Coupon22
  • 395
  • 8
  • 24
  • 1
    Can you upload that strange errors? And one advice: Don't load a new frame, make your game so you can load a new panel. – Branislav Lazic Sep 08 '12 at 12:32
  • 1
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Sep 08 '12 at 12:38
  • Why do you dispose the graphics, did you create it? The rule is always that you dispose what you created. Also, I suppose that `start` is a JFrame. It's unlikely that you need to call `getGraphics()`,... ever – Guillaume Polet Sep 08 '12 at 12:38

1 Answers1

3
  • create private/public(depends of your code design) variable for JFrame instance

  • then you can't bothering with dispose of visible JFrame and then re_create a new one

  • create class or local instace that returns JPanel with game content

  • call JFrame#getContentPane#removeAll()

  • call JFrame#getContentPane#add(new GamePanel())

  • maybe there are easiest ways how to reset whatever (depends of your code design) to the start_up statuses

mKorbel
  • 109,525
  • 20
  • 134
  • 319