Java GridBagLayout中的定位约束不起作用

Java GridBagLayout中的定位约束不起作用,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我试图使用GridBagLayout,但它可能工作不正常 这是我的代码,我不知道有什么错,但是GridBagConstraints的锚定约束不起作用,它们只是聚集在一起。如果我正确理解此布局的意图(我不确定我是否理解),那么这可以使用边界布局来完成 在这里,它具有更大的宽度和高度: 标题边框仅用于提供用于放置构件的约束的快速视觉参照。(间隔符)仅添加到中间行的3个标签中,以允许显示完整的标题边框 代码如下: import java.awt.*; import javax.swing.*;

我试图使用
GridBagLayout
,但它可能工作不正常


这是我的代码,我不知道有什么错,但是
GridBagConstraints
的锚定约束不起作用,它们只是聚集在一起。

如果我正确理解此布局的意图(我不确定我是否理解),那么这可以使用
边界布局来完成

在这里,它具有更大的宽度和高度:

标题边框仅用于提供用于放置构件的约束的快速视觉参照。
(间隔符)
仅添加到中间行的3个标签中,以允许显示完整的
标题边框

代码如下:

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

如果我正确理解此布局的意图(我不确定我是否理解),则可以使用
边界布局

在这里,它具有更大的宽度和高度:

标题边框仅用于提供用于放置构件的约束的快速视觉参照。
(间隔符)
仅添加到中间行的3个标签中,以允许显示完整的
标题边框

代码如下:

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

将组件添加到面板
p
,然后使用
add(p,gv)将其添加到框架(其内容窗格)
gv
中的约束已初始化为
gv.fill=GridBagConstraints.BOTH
,但其
weightx
weighty
保留在初始零位。因此,此面板将保持其首选大小,并且不会收到额外的空间,因此它没有额外的空间来分发自己的内容

由于所有标签都具有相同的大小,因此当没有额外空间时,它们的锚定将不起作用

当你换线的时候

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

public class BLNotClumpingExample {

    private JComponent ui = null;

    BLNotClumpingExample() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(10,10));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        ui.add(getLabel("Label 1", "PAGE_START"), BorderLayout.PAGE_START);
        ui.add(getLabel("Label 2", "PAGE_END"), BorderLayout.PAGE_END);
        ui.add(getLabel("Label 3 (spacer)", "LINE_END"), BorderLayout.LINE_END);
        ui.add(getLabel("Label 4 (spacer)", "LINE_START"), BorderLayout.LINE_START);
        ui.add(getLabel("Label 5 (spacer)", "CENTER"), BorderLayout.CENTER);
    }
    
    private JLabel getLabel(String text, String constraint) {
        JLabel l = new JLabel(text, SwingConstants.CENTER);
        l.setBorder(new TitledBorder(constraint));
        return l;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            BLNotClumpingExample o = new BLNotClumpingExample();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

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

gv.fill = GridBagConstraints.BOTH;
gv.weightx = 1;
gv.weighty = 1;
要可视化锚定的实际效果,可以将
main
方法更改为

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);
    }
}
publicstaticvoidmain(字符串[]args){
gbanchoreExample=新的gbanchoreExample();
组件网格=新JComponent(){
@凌驾
受保护组件(图形g){
g、 setColor(Color.GREEN);
int w=getWidth(),h=getHeight();
对于(int i=1;i<5;i++){
int x=(int)(w/5.0*i);
g、 抽绳(x,0,x,h);
}
}
};
e、 玻璃材质窗格玻璃(网格);
grid.setVisible(true);
e、 设置大小(720);
e、 setVisible(真);
}

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

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

由于所有标签都具有相同的大小,因此当没有额外空间时,它们的锚定将不起作用

当你换线的时候

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

public class BLNotClumpingExample {

    private JComponent ui = null;

    BLNotClumpingExample() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(10,10));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        ui.add(getLabel("Label 1", "PAGE_START"), BorderLayout.PAGE_START);
        ui.add(getLabel("Label 2", "PAGE_END"), BorderLayout.PAGE_END);
        ui.add(getLabel("Label 3 (spacer)", "LINE_END"), BorderLayout.LINE_END);
        ui.add(getLabel("Label 4 (spacer)", "LINE_START"), BorderLayout.LINE_START);
        ui.add(getLabel("Label 5 (spacer)", "CENTER"), BorderLayout.CENTER);
    }
    
    private JLabel getLabel(String text, String constraint) {
        JLabel l = new JLabel(text, SwingConstants.CENTER);
        l.setBorder(new TitledBorder(constraint));
        return l;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            BLNotClumpingExample o = new BLNotClumpingExample();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

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

gv.fill = GridBagConstraints.BOTH;
gv.weightx = 1;
gv.weighty = 1;
要可视化锚定的实际效果,可以将
main
方法更改为

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);
    }
}
publicstaticvoidmain(字符串[]args){
gbanchoreExample=新的gbanchoreExample();
组件网格=新JComponent(){
@凌驾
受保护组件(图形g){
g、 setColor(Color.GREEN);
int w=getWidth(),h=getHeight();
对于(int i=1;i<5;i++){
int x=(int)(w/5.0*i);
g、 抽绳(x,0,x,h);
}
}
};
e、 玻璃材质窗格玻璃(网格);
grid.setVisible(true);
e、 设置大小(720);
e、 setVisible(真);
}

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

anchor
将组件约束到单元格中的该位置,但单元格将处理为组件的首选大小,因此您可能不会“看见了吗“结果-由于
JPanel
不会扩展以填充框架,请举例说明。我正好有一个软件包,用于所有像这样的短代码,每个人和他的狗都想把它们叫做示例代码
example
。使其更加具体,如
GBLClumpingExample
。2) 在提供提示的同时:请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并始终如一地使用它。。。。3)
标签l1=新标签(“标签1”)应该是
JLabel l1=新的JLabel(“标签1”)(其他的等等)-不要混合Swing和AWT组件;GridBagConstraints.LINE\u开始、GridBagConstraints.CENTER和GridBagConstraints.LINE\u结束。必须使用gridx和gridy参数在页面上定位Swing组件。另外,组织代码,以便对每个Swing组件的方法调用进行分组。它使人们更容易理解您的代码。这是Oracle的教程,如何使用GridBag布局@A.