java 在没有 CardLayout 按钮的情况下在一个 JFrame 中的不同 JPanel 之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5185965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch between different JPanels in one JFrame without CardLayout-buttons
提问by belens
I'm new at Java GUI and I'm trying to make a program that shows a login screen and once logged in, a menu. What I am looking for is a way to navigate through different JPanels in just one JFrame. To navigate the user should use buttons located inside these panels.
我是 Java GUI 的新手,我正在尝试制作一个显示登录屏幕和登录后菜单的程序。我正在寻找的是一种在一个 JFrame 中浏览不同 JPanel 的方法。导航用户应该使用位于这些面板内的按钮。
I use Netbeans to do this. The furthest I got is to open one JPanel in a JFrame, I've been trying to close one and open another but it won't work because the current panel stays open.
我使用 Netbeans 来做到这一点。我得到的最远的是在 JFrame 中打开一个 JPanel,我一直试图关闭一个并打开另一个,但它不起作用,因为当前面板保持打开状态。
The reason I do not want to use the CardLayout is because I do not wish to have to tabs visible. I don't think you can hide them...
我不想使用 CardLayout 的原因是因为我不想让选项卡可见。我不认为你可以隐藏它们......
I hope you guy's can help me out,
希望大家帮帮我
thanks in advance!!
提前致谢!!
采纳答案by gcooney
CardLayout does exactly what you want and absolutely does not require you to use tabs. In fact, the java tutorial on card layoutprovides a demo that uses a combobox to change between different cards.
CardLayout 完全符合您的要求,并且绝对不需要您使用选项卡。事实上,卡片布局的java教程提供了一个使用组合框在不同卡片之间切换的演示。
回答by Thomas
回答by David Oliván Ubieto
Simply remove the panel from the contentPane
and add the new you want to show. This have to be done by code when the button is pressed and all it's ok to change to the next panel.
只需从 中移除面板contentPane
并添加您想要显示的新面板。这必须在按下按钮时通过代码完成,并且可以切换到下一个面板。