gpt4 book ai didi

JAVA无法看到面板中添加的组件

转载 作者:行者123 更新时间:2023-12-01 14:34:22 24 4
gpt4 key购买 nike

任务:从面板中删除多个控件,然后添加新控件。

问题:完成几个方法后,旧控件消失,但我看不到新控件。

代码:

    public void StartGame() {
ActionPanel.removeAll();
CreateOponentField();
ActionPanel.repaint();

}
private void CreateOponentField() {
ActionPanel.setLayout(new java.awt.GridLayout(10, 10));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
LabelArray[i][j] = new JLabel();
LabelArray[i][j].setOpaque(true);
LabelArray[i][j].setBackground(BattleShipEnumClass.ColorMap.get(GridCellState.EMPTY));
LabelArray[i][j].setBorder(new LineBorder(Color.BLACK));

ActionPanel.add(LabelArray[i][j]);

}}}

有人知道问题/错误在哪里吗?

最佳答案

如果ActionPanelJComponent的实例,您可以使用:

ActionPanel.revalidate();

否则

ActionPanel.invalidate();
ActionPanel.validate();

关于JAVA无法看到面板中添加的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605514/

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