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

查看:48
本文介绍了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.

到目前为止,我有一个带有网格布局的主游戏窗口和一个可以运行的游戏(对我来说是的!).现在我想添加上述功能.

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 拥有的 GUI 一样复杂和丰富.

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,但通过 CardLayout 在主 GUI 中交换视图 (JPanels).这可以很好地工作并且易于实现.查看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天全站免登陆