3、 使用java实现一个倒计时时钟,创建子线实现计时功能,使用异步消息机制将计时结果显示在界面上,如图,在文本框中输入数字(使用Number文本框),点击开始按钮后,下方文本框的数字每秒减1,点击停止即停止倒计时
时间: 2023-12-04 13:02:08 浏览: 100
以下是使用Java实现的一个简单倒计时时钟,使用了子线程和Swing框架的异步消息机制实现计时和更新UI界面。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CountdownTimer implements ActionListener {
private JFrame frame;
private JLabel countdownLabel;
private JTextField inputField;
private JButton startButton;
private JButton stopButton;
private int countdownTime;
private boolean isRunning;
private Thread timerThread;
public CountdownTimer() {
frame = new JFrame("Countdown Timer");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel inputLabel = new JLabel("Enter countdown time:");
inputLabel.setFont(new Font("Arial", Font.PLAIN, 18));
frame.add(inputLabel);
inputField = new JTextField(10);
inputField.setFont(new Font("Arial", Font.PLAIN, 18));
frame.add(inputField);
startButton = new JButton("Start");
startButton.setFont(new Font("Arial", Font.PLAIN, 18));
startButton.addActionListener(this);
frame.add(startButton);
stopButton = new JButton("Stop");
stopButton.setFont(new Font("Arial", Font.PLAIN, 18));
stopButton.addActionListener(this);
stopButton.setEnabled(false);
frame.add(stopButton);
countdownLabel = new JLabel("0");
countdownLabel.setFont(new Font("Arial", Font.PLAIN, 36));
countdownLabel.setPreferredSize(new Dimension(300, 100));
frame.add(countdownLabel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
if (!isRunning) {
try {
countdownTime = Integer.parseInt(inputField.getText());
} catch (NumberFormatException ex) {
countdownTime = 0;
}
if (countdownTime > 0) {
isRunning = true;
timerThread = new Thread(new CountdownTask());
timerThread.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
}
} else if (e.getSource() == stopButton) {
if (isRunning) {
isRunning = false;
try {
timerThread.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
countdownTime = 0;
countdownLabel.setText("0");
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
}
private class CountdownTask implements Runnable {
public void run() {
while (countdownTime > 0 && isRunning) {
SwingUtilities.invokeLater(new UpdateLabelTask(countdownTime));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
countdownTime--;
}
isRunning = false;
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
private class UpdateLabelTask implements Runnable {
private int time;
public UpdateLabelTask(int time) {
this.time = time;
}
public void run() {
countdownLabel.setText(String.valueOf(time));
}
}
public static void main(String[] args) {
new CountdownTimer();
}
}
```
在这个例子中,我们使用了Swing框架创建了一个GUI界面,包括一个输入框、两个按钮和一个文本标签。当用户输入需要倒计时的时间后,点击开始按钮,就会创建一个子线程,使用while循环进行倒计时,并通过SwingUtilities的invokeLater方法实现异步消息机制更新文本标签上的数字。当用户点击停止按钮时,程序会结束子线程并重置倒计时时间。
阅读全文