Java Project - how to freeze a frame

Good afternoon!

I am making a game in Java. My menu button includes a new game, HighScore, About and Quit. But before the user can go to the main game, he needs to first dial his name. 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 that I do not want to hide the main menu. I want it to depend and cannot be accessed when prompted for a player name.

The frame of my main menu is larger than the frame of the player. Of course, I can simply delete the code this.setVisible(false), but the problem is that I can still access the main menu when pressed ... I want the main menu to freeze and cannot be accessed when the player frame appears. (See image below) Please help me. Thank.

Sample image

0
source share
2

, . JDialog, JFrame , , setModal JDialog. :

public Player(JFrame owner) {
  super(owner, true); // makes the dialog modal
  // ...
}

, :

Player p1 = new Player(this);

p1.setVisible(true), .

+3
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
*/
0

Source: https://habr.com/ru/post/1788648/


All Articles