Frame within frame - java

I'm making a simple arithmetic game.
The first frame is a login screen, next this will close and then open a new frame after the user is logged in.
In the next frame, you can choose buttons which represent the game's modes and when I click a button a new frame opens.
What I am trying to accomplish is that I want is to continue working within this new frame. I don't want to keep moving between frames.
I am trying to make a back button for each mode so I can return to the original modes frame. I am only using the two frames for my program.
Is there a way I can choose the mode then go to a new screen and still be able to go back to the mode selection frame?

You could have 2 JPanels and when you press a button, an action listener does
Frame.remove(Panel1);
Frame.add(Panel2);

Related

Java Swing - How can I make a frame "mandatory to interact with"?

I'm making a game in Java Swing. Every time the player levels up I display a new frame on top of the one containing the gameplay which has some options on what rewards to get for having leveled up. Due to the nature of my rewards being able to skip choosing some of the upgrades could break the game, so I want to ensure that the user can only resume playing by clicking on one of the 1-3 buttons displayed in the level-up frame.
I have the frame set to undecorated and to appear always on top, but nothing's stopping the user from clicking outside of it to the main gameplay panel and simply pressing esc to resume playing. Instead I want it so the user can only click on the level-up frame out of the 2.
Is there an easy setting I can toggle for this, or do I have to remove remove my keyboard input listener from the first panel to achieve this?

I want to Disable Focus on Main JFrame, when I try to click on it after opening the 2nd JFrame

This Image represent the 1st/Main Frame and the 2nd Frame.The 2nd frame will open when I click on "Review Order" Button. After it's opened i want to disable focus on the main frame so the user won't able to access the main frame until he/she close the 2ndframe.
Use
mainFrame.setEnabled(false);
in order to make the Main Frame ignore user input.
Later, when the user is allowed to access the Main Frame, re-enable it by calling:
mainFrame.setEnabled(true);

Closing one JFrame while other is up

I have two JFrames opened simultaneously (by clicking certain button in the main Frame I open second Frame). I can of course exit whole program by clicking X on the main frame. The thing is: is there a way to close my subFrame by clicking X while the Main Frame remain opened? In the subFrame there is going certain action and I want to shut it down by click X but not closing the Main Frame at a time.
Write your close function like this
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

How to get JFrame's button sizes?

I created a JFrame and in the top right corner I have 3 buttons (minimize, maximize, close). How do I get the size of these buttons? I want to place a new button just to the left of the minimize button in the title bar but I need to know how much space these existing buttons take up so that I don't place my button on top of them. ie If you open a recent version of the Chrome browser you'll see a button beside the minimize button in the browser window. I want to do the same sort of thing in my Java application.
What your describing is very advanced.
An alternative route would be to use:
setUndecorated(true);
Then create your own window decorations. This way everything matches and you can add all of the button that you want.

Open New Window And Close Parent

When my program launches, a window (aka "StartWindow") opens that has 3 options: New Game/Load Game/Exit. When New Game or Load Game is clicked (and after some input), the game window (aka "GameWindow") will open so the user can play.
When the GameWindow opens, I would like to have the StartWindow close. And when the GameWindow closes, the StartWindow will open until the actual "EXIT" button is clicked.
Is there a way to do this? Right now, the only way I can achieve something similar to this is having a boolean called "gameRunning". When this is true, the buttons on StartWindow will have no action when clicked. Opposite when false.
For example purposes, suppose that each window has 3 buttons, a text field, and nothing else.
Use setVisible(false) method on parent before opening any child window. When child window closes call setVisible(true). It will solve your problem

Categories