Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: How can I implement a login screen before showing a JFrame?

I'm trying to make a little game that will first show the player a simple login screen where they can enter their name (I will need it later to store their game state info), let them pick a difficulty level etc, and will only show the main game screen once the player has clicked the play button. I'd also like to allow the player to navigate to a (hopefully for them rather large) trophy collection, likewise in what will appear to them to be a new screen.

So far I have a main game window with a grid layout and a game in it that works (Yay for me!). Now I want to add the above functionality.

How do I go about doing this? I don't think I want to go the multiple JFrame route as I only want one icon visible in the taskbar at a time (or would setting their visibility to false effect the icon too?) Do I instead make and destroy layouts or panels or something like that?

What are my options? How can I control what content is being displayed? Especially given my newbie skills?

like image 728
Katherine Rix Avatar asked Sep 06 '11 16:09

Katherine Rix


1 Answers

A simple modal dialog such as a JDialog should work well here. The main GUI which will likely be a JFrame can be invisible when the dialog is called, and then set to visible (assuming that the log-on was successful) once the dialog completes. If the dialog is modal, you'll know exactly when the user has closed the dialog as the code will continue right after the line where you call setVisible(true) on the dialog. Note that the GUI held by a JDialog can be every bit as complex and rich as that held by a JFrame.

Another option is to use one GUI/JFrame but swap views (JPanels) in the main GUI via a CardLayout. This could work quite well and is easy to implement. Check out the CardLayout tutorial for more.

Oh, and welcome to stackoverflow.com!

like image 160
Hovercraft Full Of Eels Avatar answered Sep 21 '22 16:09

Hovercraft Full Of Eels