0
我做了一個簡單的應用程序,其中包含一個測驗問題和用戶選擇一個答案,但我需要你的幫助,在我的應用程序中添加一個倒計時器20秒,當這個時間到了,它將直接轉移到下一個問題,在一次用戶回答也將轉移到下一個問題倒數計時器?
謝謝
我做了一個簡單的應用程序,其中包含一個測驗問題和用戶選擇一個答案,但我需要你的幫助,在我的應用程序中添加一個倒計時器20秒,當這個時間到了,它將直接轉移到下一個問題,在一次用戶回答也將轉移到下一個問題倒數計時器?
謝謝
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.WindowConstants;
public class Countdown extends JFrame {
// Countdown 42 seconds
public static int counterValue = 42;
public static Timer timer;
public static JLabel label;
public Countdown() {
initGUI();
}
private void initGUI(){
BorderLayout thisLayout = new BorderLayout();
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.getContentPane().setLayout(thisLayout);
label = new JLabel();
label.setText(String.valueOf(counterValue));
this.getContentPane().add(label, BorderLayout.CENTER);
this.setTitle("Countdown Example");
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
Countdown countdown = new Countdown();
Countdown.timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// =1 sec
Countdown.counterValue--;
Countdown.label.setText(String.valueOf(counterValue));
if(Countdown.counterValue == 0){
System.out.println("Counterdown ausgelaufen!");
// Timer stop
Countdown.timer.stop();
}
}
});
// Timer start
timer.start();
}
}
從http://blog.mynotiz.de/programmieren/java-countdown-und-timer-am-beispiel-von-swing-1707/兩者(德國)
的「Android的方式」做定時的事情是通過張貼Runnable
任務到Handler。
http://www.androidhub4you.com/2014/01/countdown-example-in-android-custom.html – 2014-10-11 21:55:40
Android有一個內置的類爲此http://developer.android.com/reference/android /os/CountDownTimer.html – Simon 2014-10-11 23:02:01