[英]How To Reset JFrame (Restart Game)?
This is a java game that I have started working on.I have been trying to add a button that says "RESTART" which on clicking resets the whole program the way it was when it was at the beginning( i mean at the start of the game). 这是我已经开始研究的Java游戏。我一直试图添加一个显示“ RESTART”的按钮,单击该按钮可以像按开始时一样重设整个程序(我的意思是在开始时游戏)。
Here is my code: There are 2 buttons namely "PLAY" & "CHECK WHO WON!" 这是我的代码:有2个按钮,分别是“ PLAY”和“ CHECK WHO WON!”。
For "PLAY" This is the code: 对于“ PLAY”,这是代码:
int delay = 1000;
final Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
String b = "C:\\Users\\COMPUTER\\Desktop\\deck\\.png";
Random r = new Random();
r1 = r.nextInt(upplim)+lolim;
String a = Integer.toString(r1);
String c = "C:\\Users\\COMPUTER\\Desktop\\deck\\"+a+".png";
l1.setIcon(new ImageIcon(c));
}
},delay, 50);
For "CHECK WHO WON!" 对于“检查谁赢了!” This is the code:
这是代码:
final int p = h;
System.out.println("ANSWER IS:"+p);
int delay2 = 1000;
for (int i = 1; i < 53; i++)
{
while(true)
{
next = rng.nextInt(Ulim) + Llim;
if (!generated.contains(next))
{
generated.add(next);
break;
}
}
if ( i % 2 == 0 )
{count++;
deck1[e] = next;deck1count++;
e++;
}
else {count++;
deck2[f] = next;deck2count++;
f++;
}
System.out.println(""+next);
if(next==p)
{break;}
}
if(deck1count==deck2count)
{
count=count-2;
fcard=99;}
final Timer timer2 = new Timer();
timer2.schedule(new TimerTask(){
public void run(){
do
{
System.out.println("dec2 "+deck2[z]);
String a = Integer.toString(deck2[z]);
String c = "C:\\Users\\COMPUTER\\Desktop\\deck\\"+a+".png";
l3.setIcon(new ImageIcon(c));
System.out.println("dec1 "+deck1[z]);
String b = Integer.toString(deck1[z]);
String d = "C:\\Users\\COMPUTER\\Desktop\\deck\\"+b+".png";
l4.setIcon(new ImageIcon(d));
System.out.println("count"+count);
z++;
count=count-2;
if(fcard==99&&count<0)
{l3.setIcon(new ImageIcon("C:\\Users\\COMPUTER\\Desktop\\deck\\99.png"));
}
}while(count>0&&z==p);
if(count<0)
{timer2.cancel();
reschk=11;
timer2.purge();
}
}
},delay2, 1000);
There is also another set of code which is written on the MouseClicked event of a label but I don't think it would be of much help here. 在标签的MouseClicked事件上还有另一组代码,但是我认为这对您没有太大帮助。 I have tried:
我努力了:
classname.this.dispose();
classname classname = new classname();
But it just shuts the whole program down.Is there any other way to reset the game? 但这只是关闭了整个程序,还有其他方法可以重置游戏吗? Thanks for reading.
谢谢阅读。 Any help would be appreciated.
任何帮助,将不胜感激。
You obviously have values which update so that you can represent your game during different states. 您显然具有更新的值,以便可以在不同状态下表示游戏。 If you want to restart your game, you simply need to set all of these values back to the original start values.
如果您想重新开始游戏,只需要将所有这些值都设置回原始的开始值即可。 You can write a Restart method to this.
您可以为此编写一个Restart方法。
Also, dispose() is designed to close the window. 另外,dispose()用于关闭窗口。
edit: There is no magic method you can call to reset your program. 编辑:没有魔术方法可以调用来重置您的程序。
util.Timer
, otherwise output from util.Timer
should be out of EDT . util.Timer
,否则util.Timer
输出应超出EDT 。 将游戏状态保持在不同的类中,并用新的实例替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.