Java 是否保留GridBagLayout组件顺序?
我对GridBagLayout有问题。我必须更换一个部件,但在插入新部件后,位置会发生变化。请参见以下代码作为示例。 开始时为青色和黄色(从左到右)。更换后,它是黄色和红色的。我想要的结果是红色和黄色。我如何(使用GridBagLayout)修复此问题Java 是否保留GridBagLayout组件顺序?,java,gridbaglayout,Java,Gridbaglayout,我对GridBagLayout有问题。我必须更换一个部件,但在插入新部件后,位置会发生变化。请参见以下代码作为示例。 开始时为青色和黄色(从左到右)。更换后,它是黄色和红色的。我想要的结果是红色和黄色。我如何(使用GridBagLayout)修复此问题 我认为您使用的布局不正确。 您应该使用BorderLayout而不是GridBagLayout。或者使用gridx和gridy属性设置每个面板应分配的单元格 import java.awt.BorderLayout; import java.aw
我认为您使用的布局不正确。 您应该使用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标记,否则问题可能不会被正确的人看到;)好的,与使用网格约束的GBL一样,如果我为panelAReplacement设置preferredSize,它不会影响GUI。是否可以将您的解决方案用于网格,但使用组件的首选大小(使用GBL)?我知道我可以使用weightx来解决这个问题,但我不知道weightx的固定值,因为我想使用动态计算的首选值…:(
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);
}
}