Java Swing 中的 GridBagLayout

GridBagLayout in Java Swing

我有以下问题。我有一个 JPanelGridBagLayout() 集。我使用相同的布局管理器向其中添加另一个 JPanel。我有几个不同的对象。问题是我无法设置它们,使标有2号的对象在1号对象的前面。也就是说,2号对象应该从左边开始。

代码下方:

public MainPanel() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    SecondPanel secondPanel = new SecondPanel();
    gbc.insets = new Insets(10, 20, 10, 20);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.LINE_END;
    gbc.weightx = 0.1;
    add(secondPanel , gbc);
}

class SecondPanel extends JPanel {
        public SecondPanel () {
        setLayout(new GridBagLayout());
        JPanel main = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(ope, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(opeV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(am, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(amV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(cry, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(cryV, gbc);

        gbc.anchor = GridBagConstraints.BOTH;
        main.add(addButton);

        BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.RED);
        g2d.drawLine(0, 0, 320, 240);
        g2d.drawLine(320, 0, 0, 240);
        g2d.dispose();

        mainL.setIcon(new ImageIcon(img));
        mainL.setBorder(new LineBorder(Color.RED));

        gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        add(mainL, gbc);

        add(main);

        setVisible(true);
    }
}

截图:

编辑:我解决了问题,因为此代码中的第一个错误 add(mainL, gbc); 应该是 main.add(mainL,gbc);。其次,我分别制作了 SecondPanelMainPanel 两个不同的面板。

停下来,去阅读手头的How to Use GridBagLayout and make sure you keep the JavaDocs for GridBagLayout and GridBagConstraints

GridBagLayout 是一个令人惊讶的基于 "grid" 的布局,具有灵活性。

如果您想将一个组件放在另一个组件之上,那么您将不得不告诉布局这两个组件共享同一个单元格。这就是 gridxgridy 重要的地方

所以当你做类似...

add(mainL, gbc);
add(main);

您允许布局管理器决定如何最好地布局 main,这不是您想要的,相反,您需要为两个组件提供相同的 gridx/gridy约束,例如...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new SecondPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class SecondPanel extends JPanel {

        public SecondPanel() {
            setLayout(new GridBagLayout());
            JPanel main = new JPanel(new GridBagLayout());
            main.setBorder(new LineBorder(Color.GRAY));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);

            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.LINE_START;
            main.add(new JLabel("Ope"), gbc);
            gbc.anchor = GridBagConstraints.LINE_END;
            main.add(new JLabel("opeV"), gbc);
            gbc.anchor = GridBagConstraints.LINE_START;
            main.add(new JLabel("am"), gbc);
            gbc.anchor = GridBagConstraints.LINE_END;
            main.add(new JLabel("amV"), gbc);
            gbc.anchor = GridBagConstraints.LINE_START;
            main.add(new JLabel("cry"), gbc);
            gbc.anchor = GridBagConstraints.LINE_END;
            main.add(new JLabel("cryV"), gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            main.add(new JButton("Add"), gbc);

            BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.drawLine(0, 0, 320, 240);
            g2d.drawLine(320, 0, 0, 240);
            g2d.dispose();

            JLabel mainL = new JLabel();
            mainL.setIcon(new ImageIcon(img));
            mainL.setBorder(new LineBorder(Color.RED));

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(main, gbc);
            add(mainL, gbc);
        }
    }
}

我已经离开 main opaque 并添加了边框以便您可以看到它。另请注意,组件以 FILO(反向)顺序显示

GridBagLayout 是一种艺术形式,您只需要认真对待就可以掌握

But this dont resolve my problem

你的意思是……

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(main, gbc);
gbc.gridx++;
add(mainL, gbc);