Java Swing:如何在显示JFrame之前实现登录屏幕? [英] Java Swing: How can I implement a login screen before showing a JFrame?

查看:262
本文介绍了Java Swing:如何在显示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.

到目前为止,我有一个主要的游戏窗口,其中有一个网格布局和一个有效的游戏(Yay for me!)。现在我想添加上述功能。

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.

我该怎么做呢?我不认为我想要使用多个JFrame路线,因为我一次只想在任务栏中看到一个图标(或者将其可见性设置为假影响图标?)我是否制作并销毁布局或面板或这样的事情?

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?

推荐答案

一个简单的模态对话框,例如JDialog,应该在这里运作良好。在调用对话框时,可能是JFrame的主GUI可以是不可见的,然后在对话框完成后设置为可见(假设登录成功)。如果对话框是模态的,您将确切知道用​​户何时关闭对话框,因为代码将在对话框中调用setVisible(true)的行之后继续。请注意,JDialog所拥有的GUI可以像JFrame所拥有的那样复杂和丰富。

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.

另一种选择是使用一个GUI / JFrame但交换视图(JPanels)通过CardLayout在主GUI中。这可以很好地工作并且易于实现。查看 CardLayout教程了解更多信息。

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.

哦,欢迎来到stackoverflow.com!

Oh, and welcome to stackoverflow.com!

这篇关于Java Swing:如何在显示JFrame之前实现登录屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆