java - Java 中的 JPanel 问题

标签 java swing jpanel

美好的一天!

我在重置 JPANEL 内容时遇到问题。有没有办法删除JPANEL中的所有内容?

我正在制作一个 HangMan 游戏。我需要初始化我的面板以让位于下一个级别。我的面板是使用 Netbeans GUI 创建的,一切都是固定的(甚至位置和大小)。

我的示例代码如下:

 public MainGame(JDialog owner) {
        super(owner, true);
        initComponents();
        newGame();
    }

    void newGame() {
         jPanel3.setLayout(new GridLayout(3, 9, 3, 5));
         for (char buttonChar = 'a'; buttonChar <= 'z'; buttonChar++) {
             String buttonText = String.valueOf(buttonChar);
             letterButton = new JButton(buttonText);
             letterButton.addActionListener(this);
             jPanel3.add(letterButton);
         }
    }

    void checkHame(){
         newGame();   //I CALL AGAIN TO GO TO THE NEXT LEVEL...
    }

在我的代码中,jPanel3是使用netbeans的GUI创建的。我的问题是,在第一次调用 newGame 方法时,JPanel3 充满了内容。我需要删除第二次 newGame 调用中的所有内容,否则所有内容都会增加或加倍。

任何建议都将受到高度赞赏。

再次感谢您!

干杯。

最佳答案

for循环之前执行jPanel3.removeAll()怎么样?

removeAll() 的定义:-

public void removeAll()

Removes all the components from this container.

关于java - Java 中的 JPanel 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844685/

相关文章:

java - 带有 Rally rest api 的 SSLHandshakeException

java - 这个java类路径设置正确吗?

java - 将 JButton 数组添加到 JPanel(按钮不可见)

java - str.equals ("String") 与 "String".equals(str)

java - 如何在 apache lucene 中的字符串列表之间进行百分比匹配

java - 从一组包含项目的数组中获取最佳组合

java - 如何自定义格式化程序 JFormattedTextField 以显示 IP 地址?

java - 将 HTML 样式的 JtextPane 文本中的选定单词设为粗体?

java - BFS 图遍历 - 将新节点追加到邻接矩阵

java - 在单独的类中重新绘制/更新 JPanel 时出现问题