保留GridBagLayout组件顺序?

时间:2014-08-24 09:30:56

标签: java gridbaglayout

我遇到了GridBagLayout的问题。我必须更换一个组件,但插入新组件后,位置会发生变化。请参阅以下代码作为示例。 在开始时它是CYAN和YELLOW(从左到右)。更换后,它是黄色和红色。我想要的结果是RED和YELLOW。我该如何解决这个问题(使用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);
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你没有为这个pourpose使用正确的布局。 您应该使用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);
    }
}