对许多 JPanel 进行分层并动态添加它们
我正在开发一款基于主题医院的模拟游戏,这是一款相当老的游戏。 我已经在底层工作上取得了很多进展,但是现在我开始讨论 GUI 元素,这是我以前没有做过的很多工作。 我对java还是比较陌生。 我试图创建的效果如下所示...
http://www.tubechop.com/ watch/18438
单击一个按钮,打开一个带有选项卡的面板,可以从不同的选项中进行选择,然后单击一个按钮来构建一个房间。 我相信对于“选项卡”我可以使用卡片布局? 对于实际的房间建设,我已经大致分类了。 我现在遇到的主要问题是通过单击按钮打开面板。
目前,我在上面有 1 个 JFrame 和 2 个 JPanel,主游戏面板和带有几个按钮的控制面板。
谁能告诉我一些简单的例子来说明我将如何做这样的事情? 我知道它可能真的很简单,我敢打赌你们中的一些人甚至可以直接写出代码,但我是 java 新手,到目前为止,我学到了更多关于编程的逻辑元素,而不是如何构建游戏中需要的更复杂的多层 GUI。
我知道这是一个雄心勃勃的项目,但我已经走了很长的路,甚至使用 A* 实现了自定义路径查找,我对此感到很高兴(这一切都感谢 StackOverflow 的人们!)
提前感谢您的帮助。
I'm developing a sim game based on Theme Hospital, which is quite an old game.
I've made lots of progress on the underlying workings, however now I am coming to the GUI elements, which I haven't done alot of before. I am still rather new to java.
The effect I am trying to create is like shown here...
http://www.tubechop.com/watch/18438
Click on a button, opens up a panel with tabs to select from different selections, and then click a button to build a room. I believe for the "tabs" I can use a card layout? For the actual building of rooms, I am pretty much sorted. The main problem I have right now, is getting the panel to open up on the click of a button.
At current, I have 1 JFrame and 2 JPanels ontop, the main game panel and the control panel with a few buttons.
Can anyone show me some simple example of how I would do such a thing? I know its probably really simple, and I bet some of you could even write the code off the top of your head, but I am new to java, and have been taught more about the logical elements of programming so far rather than how to build a more complex multi layered GUI like required in a game.
I know it's an ambitious project, but I have come a long way, and have even implemented custom path finding using A*, which I'm happy about (All thanks to you people here at StackOverflow!)
Thank you in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JDialogs 可以工作,但它们会在您的游戏窗口上弹出新的顶级窗口。 您可以将主游戏显示和控制面板实现为 JDesktopPane(扩展 JLayeredPane)的背景,并可以将弹出窗口设为 JInternalFrames。
人为的(但有效)示例:
JDialogs would work, but they're going to pop up new top level windows over your game window. You could implement your main game display and control panel as the background of a JDesktopPane(which extends JLayeredPane), and could make the pop ups JInternalFrames.
Contrived (but working) example:
您无需打开另一个面板。 您可能会使用包含另一个面板的 JDialog。 然后,您可以在对话框上使用 CardLayout 或 JTabbedPane。 设计选择取决于您。
我建议您首先阅读 Swing 教程 的示例所有组件。 我真的无法就程序的图形提供任何建议。 为此,您需要开始提出具体问题。 但是使用面板和组件构建对话框非常简单。
You don't open up another panel. You would probably use a JDialog that contains another panel. You could then use a CardLayout or you could use a JTabbedPane on the dialog. The design choices are up to you.
I suggest you start by reading the Swing tutorial for examples of all the components. I can't really offer any advice on the graphics of the program. For that you need to start asking specific questions. But building dialogs, with panels and components is straight forward.
按钮的处理非常简单。 您需要有一个类实现
ActionListener
接口,并且需要向您的按钮注册。 您可以在此处看到一些非常清晰的示例。对于您想要显示的面板,看起来您正在寻找的内容可以在内置于顶级 Swing 容器中的根窗格框架中找到。 这是一个教程。
根窗格框架包括玻璃窗格,它是一个透明窗格,位于顶级组件的 z 顺序堆栈的顶部。 您还有分层窗格,它可能更适合您的需求。 还有一个教程。
The button processing is pretty easy. You need to have a class implement the
ActionListener
interface, and it needs to be registered with your button. You can see some pretty clear examples here.For the panels that you want to bring up, it looks like what you are looking for can be found within the Root Pane framework built into the top-level Swing containers. Here is a tutorial.
The Root Pane framework includes the Glass Pane, which is a clear pane that sits on the top of the z-order stack of the top-level components. You also have the Layered Pane, which might suit your needs a bit more. There is a tutorial for that as well.