Java 中的 JPanel 问题
再会!
我在重置 JPANEL 内容时遇到问题。有没有办法删除JPANEL中的所有内容?
我正在制作一个绞刑吏游戏。我需要初始化我的面板以让位于下一个级别。我的面板是使用 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 调用中的所有内容,否则所有内容都会增加或加倍。
任何建议都将受到高度赞赏。
再次感谢您!
干杯。
Good day!
I have a problem in reseting the contents of my JPANEL. Is there a method in deleting all the contents of the JPANEL?
I am making a HangMan Game. And i need to initialize my panel to give way to the next level. My panel is created using Netbeans GUI and everything is fixed (even the position and size).
My SAMPLE code is as follows:
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...
}
In my code, jPanel3 is created by using the GUI of netbeans. My problem is, on the first call of newGame method, the JPanel3 is filled with contents.. I need to delete all the contents inside for my second newGame call or else all the contents will ADD UP or double up.
Any suggestions will be highly appreciated.
Thank you once again!
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
for循环
之前执行jPanel3.removeAll()
怎么样?removeAll()
的定义:-How about doing a
jPanel3.removeAll()
before thefor loop
?Definition for
removeAll()
:-此外,您可能需要调用 revalidate() &在removeAll之后重绘()
Also you may need to call revalidate() & repaint() after removeAll
您可以使用
removeAll
方法。不过,您可以考虑将按钮的状态重置回原始状态并重新使用按钮,而不是删除组件。
You can use the
removeAll
method.Rather than removing the components though, you might just consider resetting the state of the buttons back to their original and just reusing the buttons.