垂直将顶部与JPanel中新行中的每个元素对齐

时间:2014-04-07 05:38:19

标签: java swing layout

我在面板中有一个JPanel,其中包含一组其他JPanel(每个包含一个JLabel)。如何设置面板,使内部JPanel垂直对齐JPanel的顶部,每个内部面板进入一个新行。喜欢:

InnerPanel 1
InnerPanel 2
InnerPanel 3


// empty space

我尝试过设置没有布局(使用默认布局),但内部面板在同一条线上,直到线条填充。像:

InnerPanel 1 InnerPanel 2 InnerPanel 3
InnerPanel 4



// empty space

我还尝试了BoxLayout和GridBagLayout,但随后内部面板空出来占据了面板的整个垂直空间,如:

InnerPanel 1


InnerPanel 2


InnerPanel 3

代码:

public class HomeStream extends JPanel
    public HomeStream()
    {
        this.setLayout(new GridBagLayout());
        int counter = 0;

        // HomeStreamContent is the inner panel here containing a JLabel
        for (HomeStreamContent hsc : this.content)
        {
            this.add(hsc, getConstraints(0, counter++));
        }
    }

    public static GridBagConstraints getConstraints(int x, int y)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = x;
        c.gridy = y;
        c.weightx = 1.0;
        c.weighty = 0.1;
        c.insets = new Insets(10, 10, 10, 10);
        c.anchor = GridBagConstraints.PAGE_START;
        c.fill = GridBagConstraints.HORIZONTAL;

        return c;
    }

2 个答案:

答案 0 :(得分:3)

使用BoxLayout,您需要设置面板​​的maximumSizepreferredSize,因为如果您不这样,框将导致它延伸。因此,对于每个内部面板,您可以做到这一点

JPanel panel = new JPanel() {
    @Override       
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }
};

enter image description here

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

public class TestBoxLayout {

    public TestBoxLayout() {
        Box box = Box.createVerticalBox();
        for (int i = 1; i < 4; i++) {
            JPanel panel = new JPanel() {
                @Override
                public Dimension getMaximumSize() {
                    return getPreferredSize();
                }
            };
            JLabel label1 = new JLabel("Label");
            JLabel label2 = new JLabel(String.valueOf(i));
            panel.add(label1);
            panel.add(label2);
            box.add(panel);

        }

        JFrame frame = new JFrame();
        frame.add(box);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestBoxLayout();
    }
}

未设置最大值

enter image description here

导入javax.swing.Box;

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

public class TestBoxLayout {

    public TestBoxLayout() {
        Box box = Box.createVerticalBox();
        for (int i = 1; i < 4; i++) {
            JPanel panel = new JPanel();
            JLabel label1 = new JLabel("Label");
            JLabel label2 = new JLabel(String.valueOf(i));
            panel.add(label1);
            panel.add(label2);
            box.add(panel); 
        }

        JFrame frame = new JFrame();
        frame.add(box);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestBoxLayout();
    }
}

答案 1 :(得分:1)

尝试使用new BoxLayout(BoxLayout.Y_AXIS),然后添加四个面板。添加垂直胶水或刚性区域。例如:

panel.add(Box.createverticalGlue()); //responsive when resizing window

panel.add(Box.createRigidArea(new Dimension(0,100))); //not responsive to window size

使用BoxLayout的更多信息:

http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html


您也可以使用GridBagLayout执行此操作:

panel.setLayout(new GridBagLayout());
GridBagConstraints gc = GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.NORTHWEST;

gc.gridy=0;
gc.weighty = 0.1; //you might decrement this to make the panels closer to each other
panel.add(PANEL1,gc);

gc.gridy++;
panel.add(PANEL2,gc);


gc.gridy++;
panel.add(PANEL3,gc);

gc.gridy++;
gc.wieghty = 3; //you might increment this to make this last panel longer
panel.add(PANEL4,gc);

PS:我没有尝试过这段代码,但这就是概念。

有关如何使用GridBagLayout的更多信息:

http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html