如何根据按钮操作切换框架内的面板?
我创建了一个具有工具栏、菜单栏和内容区域的应用程序。 我已将菜单和工具栏添加到应用程序窗口,但我坚持根据通过工具栏单击的按钮操作(如添加、显示)在内容区域中显示面板。 这是正确的方法吗? 有没有更好的方法来做到这一点?
I have created an application that has a toolbar, menubar and content area. I have added the menu and toolbar to the application window, but I am stuck with displaying the panel in content area based on the button action (like add, display) which is clicked through the toolbar. Is this a right way to do this? Is there any better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想要有多个
JPanel
,一次只显示其中一个? 如果是这样的话,听起来就像CardLayout
就是您所需要的。 你基本上 使用唯一的名称将所有JPanel
添加到其中,然后您可以告诉CardLayout
其中哪些 show< /a>.If I understand correctly, you want to have multiple
JPanels
, only one of which is shown at a time? If that's the case, it sounds like aCardLayout
is what you need. You basically add all yourJPanels
to it with unique names, and then you can tell theCardLayout
which of them to show.我认为 CardLayout 可能是您想要的,但如果像内存使用这样的问题是一个问题,并且您确实想要删除并添加一个新的 JPanel/组件,则需要删除旧的 JPanel 并添加新的 JPanel(具有适当的约束和/或索引取决于布局)。 进行切换后,您必须 validate() 内容窗格。
I think CardLayout is probably what you want, but if something like memory usage is a concern and you really want to remove and add a new JPanel/Component, you need to remove the old JPanel and add the new one (with proper constraints and/or index depending on the layout). You'll have to validate() the content pane after you make the switch.