如何使用 BorderLayout/GroupLayout 从 JPanel 中删除特定元素?
我需要删除JPanel
的Center
中的Component
,但经过一番尝试后没有成功。
我在这里尝试了该方法:
使用以下命令从 JPanel 中删除 CENTER 元素BorderLayout
但是答案的方法会产生编译时错误:
Type mismatch: cannot convert from LayoutManager to BorderLayout
我对答案的解释是否错误?
此外,我也很好奇是否可以仅更新 GroupLayout 中的单个组件。有人可以告诉我该怎么做吗?
编辑:@mre:这是代码:
BorderLayout layout = panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
与链接中的代码基本相同。
谢谢大家!
I need to remove the Component
in the Center
of the JPanel
, but after some tries no prevail.
I tried the method here:
Removing the CENTER element from a JPanel using BorderLayout
But the answer's method produces a compile time error:
Type mismatch: cannot convert from LayoutManager to BorderLayout
Am I interpreting the answer wrong?
In addition, I am also curious if I can update just a single component from a GroupLayout. Could somebody tell me how to do it?
EDIT: @mre: Here's the code:
BorderLayout layout = panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
Which is basically the same as in the link.
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将布局管理器向下转换为 BorderLayout :
但是如果您知道哪个组件位于中心,则可以将其删除:
You have to downcast the layout manager to BorderLayout :
But if you know which component is in the center, you can just remove it :