美好的一天!
我在重置 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/