gpt4 book ai didi

java - Swing: "thin"标准组件充当容器?

转载 作者:行者123 更新时间:2023-11-30 07:38:02 25 4
gpt4 key购买 nike

我需要在 Swing 中选择一个标准容器(JPanel?),我可以将其用作占位符,我可以向其中添加另一个扩展 JPanel 的自定义组件:

JPanel containerPanel;
// built by a library from a text file, automatically part of a nice layout
MyPanel componentPanel;
// something complicated that I can't integrate with the builder library

containerPanel = builder.getMyContainerPanel();
componentPanel = new MyPanel(...);
containerPanel.add(componentPanel);

有没有办法以某种方式耦合两个面板尺寸,以便调整大小正常工作?我不太确定调整大小在 swing 中是如何工作的,但我想要的是外部 containerPanel 是一个薄包装器,它从属于我的 componentPanel 和外部面板尽可能多地代表内部小组。

我不想以这种方式做事,但这似乎是将构建器库与我的自定义组件分离的最佳方式。

最佳答案

我会简单地使用 GridLayout .

containerPanel.setLayout(new GridLayout(1, 1));

这样做的好处是你可以只添加子面板而不需要任何参数并且保证使用整个区域:

containerPanel.add(componentPanel);

关于java - Swing: "thin"标准组件充当容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2202113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com