Java 嵌套布局中的动态缩放

Java 嵌套布局中的动态缩放,java,swing,layout-manager,Java,Swing,Layout Manager,我想让包含JLabel的任意JPanel在一个固定大小的容器(JPanel)中动态扫描。比如说: __________________________________ | _____________ _____________ | | | Test Label| | Test Label| | | |___________| |___________| | |________________________________| 扩展到这个 __________________

我想让包含
JLabel
的任意
JPanel
在一个固定大小的容器(
JPanel
)中动态扫描。比如说:

__________________________________
| _____________    _____________ |
| | Test Label|    | Test Label| | 
| |___________|    |___________| |
|________________________________|
扩展到这个

__________________________________
| ________   ________   ________ |
| |Tes...|   |Tes...|   |Tes...| | 
| |______|   |______|   |______| |
|________________________________|
我尝试使用
BoxLayout
(X方向),为每个包含的
JPanel
设置最大大小:

OuterContainer.Width/NumberOfContainedJPanel

没有成功。标签周围的容器永远不会低于特定宽度。 此外,使用
GridBagLayout
效果不佳,因为我想动态添加更多容器,因此在每次添加时生成
GridBagLayout
约束不是一个好方法

有解决这个问题的好办法吗?以下是我的问题的SSCCE:

import javax.swing.JFrame;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.Dimension;

public class DynScalingExample extends JFrame {
    public DynScalingExample() {
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

        //---Panel 1
        JPanel panel = new JPanel();
        panel.setMaximumSize(new Dimension(20, 32767));
        getContentPane().add(panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblNewLabel = new JLabel("Test label");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        //---Panel 2
        JPanel panel_1 = new JPanel();
        getContentPane().add(panel_1);
        GridBagLayout gbl_panel_1 = new GridBagLayout();
        gbl_panel_1.columnWidths = new int[]{0, 0};
        gbl_panel_1.rowHeights = new int[]{0, 0};
        gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel_1.setLayout(gbl_panel_1);

        JLabel label = new JLabel("Test label");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.anchor = GridBagConstraints.WEST;
        gbc_label.gridx = 0;
        gbc_label.gridy = 0;
        panel_1.add(label, gbc_label);

        //---Panel 3
        JPanel panel_2 = new JPanel();
        getContentPane().add(panel_2);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[]{0, 0};
        gbl_panel_2.rowHeights = new int[]{0, 0};
        gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel_2.setLayout(gbl_panel_2);

        JLabel label_1 = new JLabel("Test label");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.anchor = GridBagConstraints.WEST;
        gbc_label_1.gridx = 0;
        gbc_label_1.gridy = 0;
        panel_2.add(label_1, gbc_label_1);
    }

}

正如@Andrew Thompson所建议的那样,
GridLayout
可以完美地实现这一目的。例如,请参见以下内容:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DynScalingExample extends JFrame {
    public DynScalingExample() {
        getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

        //---Panel 1
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblNewLabel = new JLabel("Test label");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        //---Panel 2
        JPanel panel_1 = new JPanel();
        getContentPane().add(panel_1);
        GridBagLayout gbl_panel_1 = new GridBagLayout();
        gbl_panel_1.columnWidths = new int[]{0, 0};
        gbl_panel_1.rowHeights = new int[]{0, 0};
        gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel_1.setLayout(gbl_panel_1);

        JLabel label = new JLabel("Test label");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.anchor = GridBagConstraints.WEST;
        gbc_label.gridx = 0;
        gbc_label.gridy = 0;
        panel_1.add(label, gbc_label);

        //---Panel 3
        JPanel panel_2 = new JPanel();
        getContentPane().add(panel_2);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[]{0, 0};
        gbl_panel_2.rowHeights = new int[]{0, 0};
        gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panel_2.setLayout(gbl_panel_2);

        JLabel label_1 = new JLabel("Test label");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.anchor = GridBagConstraints.WEST;
        gbc_label_1.gridx = 0;
        gbc_label_1.gridy = 0;
        panel_2.add(label_1, gbc_label_1);
    }

}

GridLayout
会产生这种效果(如果使用正确)。好的
GridLayout
效果很好,但是由于构造函数中的参数cols,在添加新项时是否有另一种可能代替始终创建新布局:setLayout(新的GridLayout(0,1,0,0));setLayout(新的GridLayout(0,2,0,0));setLayout(新的GridLayout(0,3,0,0));etc?
GridLayout(1,0,0,0)
?第二个参数指定列数,因此每次插入新项时,我都需要一个额外的列。这意味着每次添加项目时,我都必须重新构建整个布局。难道没有更好的解决办法吗?他们是你的朋友。0表示根据需要添加列。