Reputation: 1
import javax.swing.JFrame;
public class Driver00
{
public static void main(String[] args)
{
/*Create a frame (outside box) and write what text
will be displayed as the frame title*/
JFrame frame = new JFrame("Casino Blackjack");
JFrame frame2 = new JFrame("Rules and Information");
//allows JFrame to be maximiazed on open
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame2.setExtendedState(JFrame.MAXIMIZED_BOTH);
//give frame a size
// frame.setSize(2000,2000);
//set location on the computer screen will frame appear
//frame.setLocation(200, 150);
//use this so when you press X in corner, frame will close
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add your panel to the frame. name must match Panel class name
frame.setContentPane(new PanelProject());
frame2.setContentPane(new Rules());
// always include
frame.setVisible(true);
frame2.setVisible(true);
}
}
This is the application that launches my GUI, my problem is that if I don't comment out the line frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and I click the x
button, it closes both frame
and frame2
. It works the other way too. If I dont comment out out the same line that has frame
instead of frame2
and I click the x
button, it closes both frames.
I would like to be able to close on frame or the other without having to worry about both frames being closed.
Upvotes: 0
Views: 87
Reputation: 392
DISPOSE_ON_CLOSE or System.exit(0);
You can also just set the visibility on the click of x set it to false so it looks like it closed. Then when the application is done the x will close both
Upvotes: 1
Reputation: 1168
Why are both your JFrames
in a single class? You should separate out GUI logic so that one screen equals one class. It makes for far better management, especially if those JFrames
get more complicated.
If your frames are separated out into separate classes the following code works fine:
// this is important
mainFrame.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// mainFrame being the frame you want to close
mainFrame.setVisible(false);
mainFrame.dispose();
}
});
System.exit(0)
is used to close the entire program, which isn't what you want here.
However, if you maintain both of your JFrames
in a single class, you'd need to add some logic so that if there's only one JFrame
left, you want to close the program entirely.
That said, it might be useful to include that regardless.
I offer this as an alternative to @peeskillet's answer, as it requires less work for you. However, I recommend reading his answer and reseaching the subject yourself as it is important :)
(I use multiple JFrames
myself, and simply consider it a matter of decent object management to maintain them effectively)
EDIT:
4 :: Why I prefer setting a custom
WindowListener
instead of usingDISPOSE_ON_CLOSE
is simply because I often have custom code relating to program shutdown. I find it gives me more creative freedom.You may find
DISPOSE_ON_CLOSE
works better for you.
EDITEDIT: StackOverflow is stupid with how it handles lists.
Upvotes: 0
Reputation: 209132
"I would like to be able to close on frame or the other without having to worry about both frames being closed."
First of all you will want to take a look at The Use of Multiple JFrames, Good/Bad Practice?. Just to highlight, the majority of voters lean towards the "bad" side.
That being said, you should consider the semantics of your frames. Namely opening another frame just for game instructions. This may seem reasonable, but if you understand modality, you will probably agree that for the use of a secondary frame for this scenario, a modal JDialog
would be more suitable, as the the main game frame's interaction would be blocked, while the instruction dialog is open. See more at How to Make Dialogs
Another option, if you didn't want to use two separate windows, would be to use a CardLayout
. That would allow you to swap between views. So when the instructions are prompted, the game would move out of view, and the instructions into view. And vice versa. You can see more at How to use CardLayout. Also see a simple example here
Upvotes: 3