Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame not opening properly multiple times  RSS feed

 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me begin by first explaining a bit about my program. It is a simple game with the main commands in one class, then another class for the GUI, and another class for an intro frame.
When I start the program, it creates a small intro frame that allows the player to pick whether the game is against a player or the computer. Upon making your choice, that frame then closes
and the main game frame is created. When the game is over, you are given the option to play again, and if you choose yes, it disposes the main frame and starts the process all over again. The problem that I am having
is that for whatever reason, I cannot get the intro frame to appear again the second time around. The frame itself will generate, but the only thing on it will be the image of whatever is behind it on the desktop at the time,
not the actual components as if it has crashed. I thought it might be the VM just not keeping up, but when I step through it in the Netbeans debugger, it still does the same thing. So whenever you start a new game,
it executes this

and it will work perfectly once, but if I try it again, it will crash. Also, before I was using another JFrame to pick the opponent, I was just using a JOptionPane. When using a JOptionPane to pick the opponent, the main game
frame never had any trouble being closed and displayed multiple times.


 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonard Connoway wrote:...then another class for the GUI...

Sounds to me like you might need to repaint; but I'm no GUI expert, so I'm going to move your thread to a more appropriate forum.

Winston
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I tried to repaint the component before and after setting it to visible, but I get the same results. I also tried to repaint after waiting 5 seconds, but that did not help either.
It really seems like it is crashing or something right when it is created for the second time, but I do not understand why since the main game frame does not crash and it is much
more graphically intensive.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the application is crashing then you should be able to see a stack trace in the console which tells you why. Unless you deliberately wrote a catch-block which ignores an exception. Or unless you are running your code in such a way that you can't see the console.

But why are you creating a new JFrame for a new game? (If that's what you are doing -- it isn't quite clear to me.) Why not just use the same JFrame and reset all the components to the start-of-game state?
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I am doing. When I start the main class, it creates a JFrame for the intro frame. After I use it to pick the opponent, I just set it to invisible until I call the new game method again. It doesn't create another frame though, it just sets the frame to visible again, but it doesn't work.

Also, the program is not actually crashing when the Frame is set to visible again. I stepped through it in the debugger, and after it says to set the frame to visible, the frame appears but has a blank background, but
the program will continue to run as if the frame appeared properly.

I just discovered something else. I tried to just not close the intro frame at all, and instead I just moved it to another part of my screen out of the way. Then, when I start a new game, even though the intro frame never disappeared, the buttons on it become inactive.
It seems like by closing the game frame which is in the GUI class, somehow it is messing up the intro frame which is in another class. I checked the game frame, and it's default close operation is do nothing, so I'm not sure why it would affect the other one. Also,
just setting the game frame to invisible and not disposing it yields the same result.

I finally figured it out. The program was waiting for input while I was trying to create the new game, so I just had to rewrite the main method to better handle the ending of games.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!