Exiting a frame

Thread Starter

jakegwood

Joined Apr 13, 2011
29
I attached my code to this post. I couldn't upload java files, so it's a txt, if you want to see it, just make sure you change it to java before compilation.

Anyway, it's a fully functional Connect 4 game (the edit name buttons and quit button on the frame that pops up when you win don't work yet though). I got this far with it, but I just don't know how to automatically exit a frame. Basically if you win the game, a screen comes up saying "Player 1/2 Wins" It has two buttons, play again, and quit (which, as I said, doesn't work)

when you hit play again, the game resets as it should, but how the screen that says 'player 1/2 wins' is still there. When I click play again, I want not only the game to reset (which it does fine), but the screen that win screen should disappeat. I want to do this from the action listener attached to the playagain button.

I tried searching, but that doesn't work well, because I can't define my problem in a couple keywords

Thanks

Jake
 

Attachments

sbixby

Joined May 8, 2010
57
From a pro Java programmer....

I think you made this a lot harder than needed by making everything a class on it's own. Ideally, a dialog like "WinFrame" would be self-contained, including all it's buttons and interactions.

So if I were going to rearrange just this part, I would just new up JButtons in your WinFrame class, and make their action listeners a member of the WinFrame class as well.

In those action listeners, you can simply say "this.close()" to close the window when it's done.

On a more-ideal basis, you'd arrange it so the code which invokes the WinFrame will tell it to show itself modally (meaning it stays on top and nothing underneath it can catch events), until one of the buttons are clicked. After returning from the modal call, you interpret what button was clicked and either reset the game or close the application.

I greatly respect your efforts to make this game, but I'd also like to encourage you to study some popular patterns for using Swing - modal dialog boxes, and normal layout patterns. Your main application shouldn't have to care about all of the details of the dialog box - it should be able to say, effectively "show this dialog box and return it's results to me". Then the dialog box stands on it's own. And the main program, managing the game grid, stands on it's own too. the only connection between the two would be the result that the dialog box gives back when it closes.
 

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Definitely need to study this more. I'm in intro to compsci in college, and I've been fine with all the terminal based assignments he's given us, and then this was our first gui assignment (not to mention also our final project). And I was thinking... We've never done gui before, and now we have to design an entire game? So yeah, I'm sure it's very clear that my code is bulky and awkward. I really like gui so far, but I wish he didn't throw this at us all at once :/

Modally was one of the words I was looking for. I wanted to use that for the name edit buttons, so players can't be editing names and playing the game at the same time, but I imagine that's a little complicated?

What do you mean "new up JButtons"? The way it is, the this.close() does not work when I just put it at the bottom of the actionListener, which makes sense, because 'this' does not refer to the frame that should be closed, correct?

Overall, time is a bit of a problem, as I said it took me so long to get here.... figuring out gui from scratch in about 4 days was intense... that I am left so little time (it's due tomorrow at 11:59PM) for the finer details. Next semester is data structures though, so hopefully that will help my style and organization get better.

Thanks for your help!
 

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Just got it! again... probably clumsy, but it works...

I made WinFrame a variable in the GameBoard class (which has, somewhat accidentally become pretty much the globally accessible and modified class) and then in the play again aciton listener I added Pconnect4.mainFrame.gameBoard.winFrame.dispose();

So yeah, Data Structures will help haha. Hopefully I won't have those crazy paths to get to values...
 

sbixby

Joined May 8, 2010
57
Since you got it working, I'll just answer a couple questions...

"new-up JButton" - you don't need to override button classes to use them, unless you want to add behavior to them that the base class doesn't provide.

So in the context of a dialog-box class, you can just say something like:

class xyz extends JFrame implements ActionListener {
xyz() { // constructor
btn1 = new JButton("Close");
btn1.addActionListener(this);
// other btn1 stuff like location, yada
this.add(btn1);
}

void btn1Listener(ActionEvent e) {
if (e.getSource()==btn1) {
this.close();
}
}

.....
}


So when you invoke the dialog class, it will create the buttons, and hook up the listener to the dialog object. Clicking the button calls the action listener, and you will have the context of the dialog there ('this') to be able to call close on it.

And so forth.

This is very rough stuff, though, there's newer, cleaner, nicer ways to do GUI. NetBeans has some UI building support that works pretty well (and is free), well worth using it instead of hand-coding all of the UI yourself.

That said, I don't do much UI these days - the bulk of my work is behind the scenes in app servers.
 
Top