我制作了一个包含测验问题的简单应用程序,用户选择了答案,但我需要你的帮助,在我的应用程序中添加倒数计时器,持续20秒,此时间结束时,它将直接转移到下一个问题,当用户及时回答时,它将转移到下一个问题
谢谢
答案 0 :(得分:1)
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/(德语)
答案 1 :(得分:0)
" Android方式"定时工作是将Runnable
任务发布到Handler。