嵌套布局中的动态缩放

时间:2014-02-24 12:58:29

标签: java swing layout-manager

我想在具有固定大小的容器(JPanels)中动态地包含JLabel个空闲的JPanel。所以像这样:

__________________________________
| _____________    _____________ |
| | Test Label|    | Test Label| | 
| |___________|    |___________| |
|________________________________|

缩放到此

__________________________________
| ________   ________   ________ |
| |Tes...|   |Tes...|   |Tes...| | 
| |______|   |______|   |______| |
|________________________________|

我尝试使用BoxLayout(X-Orientation),为每个JPanel设置最大尺寸:

  

OuterContainer.Width / NumberOfContainedJPanels

没有成功。标签周围的容器永远不会低于特定宽度。 使用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);
    }

}

1 个答案:

答案 0 :(得分: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);
    }

}