【问题标题】:Swing: "thin" standard component to act as a container?Swing:充当容器的“瘦”标准组件?
【发布时间】:2010-02-04 18:21:35
【问题描述】:

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

有没有办法以某种方式将两种面板尺寸结合起来,以便调整大小正常工作?我不太确定在摇摆中调整大小是如何工作的,但我想要的是外部containerPanel 是一个薄包装,它服从于我的componentPanel,并且外部面板尽可能多地委托给内部面板。

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

【问题讨论】:

    标签: java swing jpanel


    【解决方案1】:

    我只需使用GridLayout

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

    这样做的好处是可以不带任何参数只添加子面板,保证使用整个区域:

    containerPanel.add(componentPanel);
    

    【讨论】:

      【解决方案2】:

      您可以使用BorderLayout 并将您的委托容器添加到BorderLayout.CENTER 位置。

      【讨论】:

        【解决方案3】:

        嗯。好吧,我决定重写我的组件,因此它不是扩展 JPanel(继承)的类,而是使用组合并使用空 JPanel 作为参数构造 + 它将子组件添加到 JPanel。所以我可以使用构建器库来构建空的 JPanel,然后我将它传递到我自己的组件的构造函数中,所以现在我有 1 个 JPanel 而不是我必须保持耦合在一起的两个。

        【讨论】:

          猜你喜欢
          • 2014-09-11
          • 1970-01-01
          • 2016-08-26
          • 2011-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-04
          相关资源
          最近更新 更多