如何在 JFrame 中显示/隐藏 JPanel?
我正在开发的应用程序是一个游戏。我想要做的是让 JPanel 出现在 JFrame 中,就像文本或消息窗口一样,然后在不再使用时消失。我在 Netbeans 中将这些 JPanel 设计为外部类,并希望能够在 actionPerformed() 方法中调用它们。 JOptionPanes 或其他弹出对话框不是一个选项,因为它们会分散游戏的焦点。我还看到有人在类似的问题中建议使用 CardLayout。这不是我想要的,因为我不只是想交换窗格。当程序告诉他们时,他们应该离开。我该如何做到这一点,例如将其绑定到 JButton 操作?
The application I am developing is a game. What I want to do is have JPanels that appear in the JFrame, like a text or message window, and then disappear when they are no longer used. I have designed these JPanels in Netbeans as external classes and want to be able to call them in an actionPerformed() method. JOptionPanes or other popup dialogs are not an option because they take the focus away from the game. I also saw someone suggest a CardLayout in a similar question. This is not what I want because I am not just trying to swap the panes. They should go away when the program tells them to. How would I do this, say by binding it to a JButton Action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过调用
setVisible(false)
来隐藏 JPanel。例如:You can hide a JPanel by calling
setVisible(false)
. For example:如果您想在单击按钮时隐藏面板,请在 JButton Action 中编写以下代码。
我假设您想隐藏 jpanel1。
If you want to hide panel on button click, write below code in JButton Action.
I assume you want to hide jpanel1.
调用
parent.remove(panel)
,其中parent
是您想要放置框架的容器,panel
是您想要添加的面板。Call
parent.remove(panel)
, whereparent
is the container that you want the frame in andpanel
is the panel you want to add.