我需要在 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/