gpt4 book ai didi

java - 倒计时器?

转载 作者:行者123 更新时间:2023-12-01 12:26:08 24 4
gpt4 key购买 nike

我制作了一个简单的应用程序,其中包含测验问题,用户选择一个答案,但我需要您的帮助在我的应用程序中添加一个 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/ (德语)

关于java - 倒计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26319616/

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