[英]Print 60 seconds countdown in Java
我到处搜索了一些简单的代码来打印60秒,例如在JFrame构造函数上。 因此,当运行JFrame时,窗口标题将显示从60-0秒倒计时(在我的情况下将关闭)。 -无需关闭代码。
在以下方面:
JFrame frame = new JFrame("Window will terminate in: " + java.util.Calendar.SECOND);
当然,上面的代码没有意义,因为它以秒为单位打印当前时间。 但是你明白了。
使用TimerTask
每秒设置JFrame
的标题。
public class TimerFrame {
private static JFrame frame = new JFrame();
public static void main(String[] args) {
TimerFrame timerFrame = new TimerFrame();
timerFrame.frame.setVisible(true);
timerFrame.frame.setSize(400,100);
new Timer().schedule(new TimerTask(){
int second = 60;
@Override
public void run() {
frame.setTitle("Application will close in " + second-- + " seconds.");
}
},0, 1000);
}
}
尝试这个:
int count = 60;
while(count != 0){
try {
countlabel.setText(String.valueOf(count));
count--;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.