我创建了一个从 JPanel 扩展的类,其布局属性是 GridBagLayout。我基本上需要显示一组 JLabels(网格),但是一旦创建了组件,我就无法替换它。我尝试删除要更改的组件,然后放置新组件,但结果很奇怪。例如,我创建了一个 10x10 JLabels 数组,并希望用以下代码替换 position[0][0] 和 position[9][9]:
//At first i have the jpanel layout completely filled
this.remove(0);
this.add((JLabel) start, 0); //This was created with GridBagLayout.gridx = 0 and GridBagLayout.gridy = 0 and it's fine
this.remove(99);
this.add((JLabel) end, 99); //This was created with GridBagLayout.gridx = 9 and GridBagLayout.gridy = 9 and it's out of grid
this.revalidate();
但只有 position[0][0] 看起来不错。我应该怎么做才能更换一个组件?