Java Project - how to freeze Frame
Good day!
I am doing a game in Java. My menu button includes New Game, HighScore, About and Quit. But before the user can proceed to the main game, he needs to type his name first. I used this code as follows:
private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt) {
Player p1 = new Player();
this.setVisible(false); // I must replace this code
p1.setVisible(true);
}
My problem is, I don't want to make the main menu hidden. I want it to freeze and cannot be accessed when the player name is being asked.
My Main me开发者_如何转开发nu frame is bigger than the player frame.. Of course, I can just delete the code this.setVisible(false)
but the problem is I can still access the main menu when clicked... I want the main menu to freeze and cannot be accessed when the player frame pops up.. (See image below) Please help me. Thank you.
What you want to do is make your player frame a modal dialog. You would want to make it a subclass of JDialog rather than JFrame
or whatever you're using and set it to be modal either using its setModal
method or with one of JDialog
's constructors. For example:
public Player(JFrame owner) {
super(owner, true); // makes the dialog modal
// ...
}
Then you could create the dialog from the main frame like:
Player p1 = new Player(this);
When you call p1.setVisible(true)
, the main frame will be blocked and unclickable.
private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt)
{
Player p1 = new Player();
p1.setVisible(true);
setEnabled(false);
}
/*
setEnabled(boolean b) (java.awt.Component)
Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially bydefault.enter code here
*/
精彩评论