gpt4 book ai didi

java - 保留 GridBagLayout 组件顺序?

转载 作者:行者123 更新时间:2023-12-01 12:37:20 28 4
gpt4 key购买 nike

我在使用 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);
}
}

关于java - 保留 GridBagLayout 组件顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470318/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com