带有SpringLayout的JPanel不可见

时间:2016-07-19 18:37:13

标签: java swing jpanel layout-manager springlayout

我疯了!我通过一种方法将JPanel添加到另一个JPanel。此方法通过SpringLayout生成网格。问题是在将所有JComponents添加到面板后,面板的首选大小为0,因此不显示任何内容。使用另一个布局管理器解决了可见性,但我需要SpringLayout的结构。我找不到我的问题。

private JPanel panelErzeugen(ArrayList<JComponent> labels){
    JPanel panel = new JPanel();

    SpringLayout layout = new SpringLayout();
    panel.setLayout(layout);

    int abstandVert = 5;
    int abstandHori = 5;

    for(JComponent label : labels){
        if(labels.indexOf(label) == labels.size()/2){
            abstandVert = 100;
            abstandHori = 5;
        }

        layout.putConstraint(SpringLayout.WEST, panel, abstandVert, SpringLayout.WEST, label);
        layout.putConstraint(SpringLayout.NORTH, panel, abstandHori, SpringLayout.NORTH, label);

        panel.add(label);

        abstandHori = abstandHori + 25;
    }

    return panel;
}

0 个答案:

没有答案