删除 JPanel 内的 JPanel...JPanel 内
我正在尝试使用 NetBeans 在 Java 中制作一个类似幻灯片的应用程序。
我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一页),以及一个 JPanel (mainPanel
),它充当应用程序“幻灯片”的支架。我还使用自定义 JPanel (SlidePanel
) 对象,然后将其放置在 mainPanel
内。其中一些 SlidePanel
对象中包含 JPanel。
现在,我已经能够将这些 SlidePanel
对象放置在 mainPanel
中,并在它们之间导航(例如,如果我单击下一个按钮,SlidePanel 1
将被 SlidePanel 2
替换,或者如果我单击后退按钮,SlidePanel 2
将被 SlidePanel 1
替换。)我通过使用 removeAll()
、revalidate()
和 repaint()
方法来完成此操作。
问题从这里开始。第三个 SlidePanel
对象内部包含一个 JPanel。当您到达第三个 SlidePanel
时会发生什么,它工作正常。但从那里开始,如果您单击后退按钮(返回到 SlidePanel 2
),SlidePanel 3
中的 JPanel 仍保留在屏幕上,其中的组件位于 >SlidePanel 2
调整以适合 mainPanel
。
我应该怎么做才能解决这个问题?
编辑:非常感谢所有回答的人。我在一个虚拟项目上尝试了 CardLayout,效果很好。现在,我所要做的就是将其添加到原来的内容中。再次感谢。 :)
I am trying to make a slideshow-like application in Java using NetBeans.
I have a JFrame (the main window) and inside it are two buttons (back and next) for navigation, and a JPanel (mainPanel
) which acts as holder of the 'slides' of the app. I am also using custom JPanel (SlidePanel
) objects which are then placed inside the mainPanel
. Some of these SlidePanel
objects have JPanels within them.
Now, I've been able to place these SlidePanel
objects inside the mainPanel
and also navigate through them (for example, if I click the next button, SlidePanel 1
would be replaced by SlidePanel 2
, or if I click the back button, SlidePanel 2
would be replaced by SlidePanel 1
.) I've done this by using the removeAll()
, revalidate()
, and repaint()
methods.
The problem starts here. The third SlidePanel
object contains a JPanel inside it. What happens when you get to the third SlidePanel
, it works fine. But from there, if you click the back button (which takes you back to SlidePanel 2
), the JPanel from SlidePanel 3
remains on the screen, with the components inside SlidePanel 2
adjusted to fit the mainPanel
.
What should I do to fix this?
EDIT: Thank you very much to all those who answered. I tried CardLayout on a dummy project and it worked fine. Now, all I have to do is add it to the original one. Thanks again. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 CardLayout ,而不是添加和删除
CardLayout
的Runtime
示例中的JPanels
并与您的想法一起可以是 此处I suggest to use CardLayout for that, rather as add and remove
JPanels
on theRuntime
example forCardLayout
and together with your idea could be here我建议您使用不同的布局管理器(例如
CardLayout
);让布局管理器担心如何处理它的组件。I suggest you use a different layout manager (e.g.
CardLayout
); let the layout manager worry about juggling it's components.