GridBagLayout 元素在网格 Y 中重叠

GridBagLayout elements are overlapped in grid Y

我正在学习如何使用 Java Swing 中的 GridBagLayout...

在此示例中,我想在每一行中放置 4 个元素,如您在我的代码中所见:

        this.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        //constraints.weighty = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = constraints.HORIZONTAL;
        this.add(label1, constraints);
        constraints.weighty = 0.0; 
        
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 0;
        constraints.gridheight = 1;
        constraints.weighty = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = constraints.BOTH;
        this.add(label, constraints);
        constraints.weighty = 0.0; 
        
        JLabel precio = new JLabel();
        precio.setText("Precio    12345.5");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        constraints.gridheight = 2;
        constraints.weighty = 1;
        constraints.fill = constraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.NORTH;
        this.add(precio, constraints);
        constraints.weighty = 0.0; 
        
        JLabel cantidad = new JLabel();
        cantidad.setText("Cantidad    ");
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 2;
        constraints.gridheight = 2;
        constraints.weightx = 1;
        constraints.fill = constraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.NORTH;
        this.add(cantidad, constraints);
        constraints.weighty = 0.0; 
        
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(100 , 30));
        constraints.gridx = 2;
        constraints.gridy = 3;
        constraints.gridwidth = 2;
        constraints.gridheight = 2;
        constraints.weighty = 1;
        constraints.fill = constraints.HORIZONTAL;
        this.add(textfield, constraints);
        constraints.weighty = 0.0; 
        
        JButton boton = new JButton();
        boton.setText("Agregar");
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 2;
        constraints.gridheight = 2;
        constraints.weightx = 1;
        constraints.fill = constraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.NORTH;
        this.add(precio, constraints);
        constraints.weighty = 0.0; 

如您在代码中所见。

在第0行,我想放一个JLabel

在第 1 行,我想放置一个带有图标的 JLabel

在第 2 行,我想放一个 JTextField

第3行,我想在右边放一个JTextfield和JTextField 边

最后在第 4 行,我想在中间放置一个 JButton 列。

但是当我 运行 JFrame:

时我得到了这个

从第二行开始,组件重叠了,我不知道为什么。

也许我在 GridBagLayout 行为中遗漏了一些东西,因为我仍在学习如何使用它。

Oracle 有一个有用的教程,Creating a GUI With Swing。跳过使用 NetBeans 学习 Swing IDE 部分。

您的代码有几个语法错误。修复这些问题后,我 运行 你的代码。

我不确定你到底想要什么。根据您的描述,我修改了您的代码以生成以下 GUI。

当使用GridBagLayout时,有些参数你设置一次,其他参数你为每个Swing组件修改。有关详细信息,请参阅 Oracle 教程 How to use GridBagLayout

下一次,post 可以将可运行代码复制到 IDE 并进行测试。

这是我使用的完整可运行代码。

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridBagLayoutExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GridBagLayoutExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Cell-33");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.anchor = GridBagConstraints.LINE_START;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(0, 10, 5, 10);
        constraints.gridwidth = 1;
        constraints.gridheight = 1;

        constraints.gridx = 0;
        constraints.gridy = 0;
        JLabel label1 = new JLabel("Label1");
        panel.add(label1, constraints);

        constraints.gridx++;
        JLabel label = new JLabel("Label");
        panel.add(label, constraints);

        constraints.gridx++;
        JLabel precio = new JLabel();
        precio.setText("Precio    12345.5");
        panel.add(precio, constraints);

        constraints.gridx++;
        JLabel cantidad = new JLabel("Cantidad");
        panel.add(cantidad, constraints);

        constraints.gridx++;
        JTextField textfield = new JTextField(20);
        panel.add(textfield, constraints);

        constraints.gridx++;
        JButton boton = new JButton();
        boton.setText("Agregar");
        panel.add(boton, constraints);

        return panel;
    }

}