gpt4 book ai didi

java - 如何使用eclipse在后台运行倒计时

转载 作者:行者123 更新时间:2023-12-01 10:57:13 27 4
gpt4 key购买 nike

这就是问题所在。倒计时运行时我无法单击按钮。我必须等到倒计时停止。我的问题是如何使倒计时在后台运行。或任何建议。请帮助我!

    public static void main(String args[]){
scrbutton myWindow = new scrbutton(); //set window design
myWindow.setSize(300,70);
myWindow.setVisible(true);
myWindow.setResizable(false);

}

public scrbutton() {

super("Clicker"); //Title
setLayout(new FlowLayout());
addWindowListener(this);
add(kotak);
add(kotak2); //add and design you components
add(kotak3);
add(enter);
enter.addActionListener(this);
kotak.setText("0");
kotak2.setText("Times remaining: 60");
kotak.setEditable(false);
kotak2.setEditable(false);
kotak3.setEditable(false);

}


public void actionPerformed(ActionEvent e) //What will run through the program?
{



click++;
kotak.setText("\r"+click); //display number of click
if (click >=10){
kotak3.setText("You Win!");
enter.setEnabled(false);
}else{
kotak3.setText("try again");
}

for(int x=60; x>=0; x--)

{System.out.print(x+"\r"); // use print than println if you use (/r).
try {Thread.sleep(100);} // 1000ms=1second thus its sleep(delay) 1 second between each iteration.
catch (InterruptedException e1){}

}
}


public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}

public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}

}

最佳答案

我要在这里冒险,因为我不做Java,但我想说你需要让应用程序是多线程的 - 你需要你的主线程不被阻塞定时器的执行线程。创建一个带有回调的新线程以增加计时器,启动线程,然后在不再需要时终止线程。

关于java - 如何使用eclipse在后台运行倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33573454/

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