首页 > 解决方案 > GridBagLayout 中的锚定约束不起作用

问题描述

import java.awt.*;
import javax.swing.*;

public class GBLClumpingExample extends JFrame{

    GBLClumpingExample(){
        GridBagLayout g = new GridBagLayout();
        GridBagConstraints gv = new GridBagConstraints();

        GridBagLayout b = new GridBagLayout();
        GridBagConstraints gc = new GridBagConstraints();

        setVisible(true);
        setSize(720,720);
        setLayout(g);

        JPanel p = new JPanel();
        p.setLayout(b);
        gv.fill = GridBagConstraints.BOTH;
        add(p,gv);
        
        Label l1 = new Label("Label 1");
        Label l2 = new Label("Label 2");
        Label l3 = new Label("Label 3");
        Label l4 = new Label("Label 4");
        Label l5 = new Label("Label 5");
        
        gc.weightx =1.0;
        gc.weighty = 1.0;
        gc.gridx= 1;
        gc.gridy= 1;
        gc.anchor = GridBagConstraints.PAGE_START;
        gc.gridx= -1;
        gc.gridy= 0;
        p.add(l1,gc);
        gc.anchor = GridBagConstraints.SOUTH;
        p.add(l2,gc);
        gc.anchor = GridBagConstraints.EAST;
        p.add(l3,gc);
        gc.anchor = GridBagConstraints.WEST;
        p.add(l4,gc);
        gc.anchor = GridBagConstraints.CENTER;
        p.add(l5,gc);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        GBLClumpingExample e = new GBLClumpingExample();
    }
}

我试图使用GridBagLayout,但它可能无法正常工作。

这是我的代码,我不知道出了什么问题,但是锚约束GridBagConstraints不起作用,它们都只是聚集在一起。

标签: javaswinglayout-managergridbaglayout

解决方案


您正在将组件添加到面板p,然后使用add(p,gv);. 中的约束gv已用 初始化gv.fill = GridBagConstraints.BOTH;,但其weightxweighty保留在初始零处。因此,此面板将保持其首选大小并且不会获得额外空间,因此它没有额外空间来分发其自己的内容。

由于所有标签的大小相同,因此在没有额外空间时,它们的锚点无效。

当你改变线路时

gv.fill = GridBagConstraints.BOTH;

gv.fill = GridBagConstraints.BOTH;
gv.weightx = 1;
gv.weighty = 1;

你会看到锚的效果。或者,您可以摆脱附加面板。还有其他冗余操作。您可以将代码简化为:

import java.awt.*;
import javax.swing.*;

public class GBAnchorExample extends JFrame{
    GBAnchorExample() {
        Container c = super.getContentPane();
        c.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = gc.weighty = 1.0;

        JLabel l1 = new JLabel("Label 1");
        JLabel l2 = new JLabel("Label 2");
        JLabel l3 = new JLabel("Label 3");
        JLabel l4 = new JLabel("Label 4");
        JLabel l5 = new JLabel("Label 5");

        gc.anchor = GridBagConstraints.PAGE_START;
        c.add(l1,gc);
        gc.anchor = GridBagConstraints.SOUTH;
        c.add(l2,gc);
        gc.anchor = GridBagConstraints.EAST;
        c.add(l3,gc);
        gc.anchor = GridBagConstraints.WEST;
        c.add(l4,gc);
        gc.anchor = GridBagConstraints.CENTER;
        c.add(l5,gc);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        GBAnchorExample e = new GBAnchorExample();
        e.setSize(720,720);
        e.setVisible(true);
    }
}

要可视化锚点的实际效果,您可以将main方法更改为

public static void main(String[] args){
    GBAnchorExample e = new GBAnchorExample();
    Component grid = new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(Color.GREEN);
            int w = getWidth(), h = getHeight();
            for(int i = 1; i < 5; i++) {
                int x = (int)(w/5.0*i);
                g.drawLine(x, 0, x, h);
            }
        }
    };
    e.setGlassPane(grid);
    grid.setVisible(true);
    e.setSize(720,720);
    e.setVisible(true);
}

这将绘制一个绿色网格以显示包含标签的逻辑单元格,因此锚点如何影响标签在其单元格中的位置变得很明显。


推荐阅读