保留griddbaglayout组件顺序



我有GridBagLayout问题。我必须更换一个组件,但在插入新组件后,位置发生了变化。请参阅以下代码作为示例。一开始是青色和黄色(从左到右)。更换后为黄色和红色。我想要的结果是红色和黄色。我怎么能解决这个问题(与GridBagLayout)?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GBLTest extends JFrame
{
    public static void main(String [] args)
    {
        new GBLTest();
    }
    JPanel panelA;
    JPanel panelB;
    JPanel panelAReplacement;
    GBLTest()
    {
        this.setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        cons.weightx = 1.0;
        cons.weighty = 1.0;
        cons.fill = GridBagConstraints.BOTH;
        panelA = new JPanel();
        panelA.setBackground(Color.CYAN);
        panelB = new JPanel();
        panelB.setBackground(Color.YELLOW);
        panelAReplacement = new JPanel();
        panelAReplacement.setBackground(Color.RED);

        cons.anchor = GridBagConstraints.EAST;
        this.add(panelA, cons);
        cons.anchor = GridBagConstraints.WEST;
        this.add(panelB, cons);
        GridBagConstraints oldCons = ((GridBagLayout) this.getContentPane().getLayout()).getConstraints(panelA);
        this.remove(panelA);
        this.add(panelAReplacement, oldCons);
        this.setSize(new Dimension(200, 200));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

我认为你没有使用正确的布局。你应该使用BorderLayout而不是GridBagLayout。或者使用gridx和gridy属性来设置每个面板应该分配的单元格。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GBLTest extends JFrame
{
    public static void main(String [] args)
    {
        new GBLTest();
    }
    JPanel panelA;
    JPanel panelB;
    JPanel panelAReplacement;
    GBLTest()
    {
        this.setLayout(new GridBagLayout());
        GridBagConstraints consA = new GridBagConstraints();
        consA.weightx = 1.0;
        consA.weighty = 1.0;
        consA.fill = GridBagConstraints.BOTH;
        consA.gridx = 0;
        consA.gridy = 0;
        GridBagConstraints consB = new GridBagConstraints();
        consB.weightx = 1.0;
        consB.weighty = 1.0;
        consB.fill = GridBagConstraints.BOTH;
        consB.gridx = 1;
        consB.gridy = 0;
        panelA = new JPanel();
        panelA.setBackground(Color.CYAN);
        panelB = new JPanel();
        panelB.setBackground(Color.YELLOW);
        panelAReplacement = new JPanel();
        panelAReplacement.setBackground(Color.RED);

        consA.anchor = GridBagConstraints.EAST;
        this.add(panelA, consA);
        consA.anchor = GridBagConstraints.WEST;
        this.add(panelB, consB);
        GridBagConstraints oldCons = ((GridBagLayout)     this.getContentPane().getLayout()).getConstraints(panelA);
        this.remove(panelA);
        this.add(panelAReplacement, oldCons);
        this.setSize(new Dimension(200, 200));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

最新更新