【问题标题】:Vertical Align top with each element in a new row in JPanel垂直对齐顶部与JPanel中新行中的每个元素
【发布时间】:2014-05-19 05:16:48
【问题描述】:

我在面板中有一个 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;
    }

【问题讨论】:

  • 尝试使用来自 SwingLabs 的 VerticalLayout 经理 SwingX

标签: java swing layout


【解决方案1】:

使用BoxLayout,您需要将面板的maximumSize 设置为preferredSize,因为如果您不这样做,框会使其拉伸因此对于每个内部面板,您都可以这样做

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

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();
    }
}

不设置最大值

导入 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();
    }
}

【讨论】:

    【解决方案2】:

    尝试使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多