
本文旨在解决java swing应用程序中gui界面无法实时刷新时间或倒计时的问题。通过深入解析swing ui更新机制,我们将介绍如何利用`javax.swing.timer`组件,在不阻塞事件调度线程(edt)的前提下,实现高效、平滑且线程安全的实时时间显示与倒计时功能。
在Java Swing应用程序中,所有与UI相关的操作(如组件的创建、修改、绘制)都必须在事件调度线程(Event Dispatch Thread, EDT)上执行。直接在主线程或自定义线程中调用Thread.sleep()来暂停程序以等待时间流逝,然后尝试更新UI,会导致以下问题:
因此,为了实现实时刷新功能,我们需要一种机制,既能周期性地触发UI更新,又不会阻塞EDT,并且确保更新操作始终在EDT上执行。
javax.swing.Timer是Swing提供的一个专门用于处理周期性任务的工具,它完美地解决了上述问题。Swing Timer的特点是:
我们将通过一个示例来演示如何使用javax.swing.Timer在Swing GUI中显示实时系统时间。
立即学习“Java免费学习笔记(深入)”;
首先,我们需要一个辅助类来获取并格式化当前系统时间。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentTime {
public String SystemDate(){
// 获取系统默认时区当前时间
LocalDateTime now_time = LocalDateTime.now();
// 对时间进行相应的格式化
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
// 返回对应的信息
return now_time.format(ofPattern);
}
}说明:
接下来,我们将构建主GUI界面,并在其中集成Swing Timer来实现时间的实时刷新。
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer; // 导入Swing Timer
public class CountdownTimerGUI {
public static void main(String[] args) {
// 使用SwingUtilities.invokeLater确保GUI创建和更新都在EDT上执行
SwingUtilities.invokeLater(() -> {
new CountdownTimerGUI().new MainGUI().showGUI();
});
}
// MainGUI作为内部类,方便在一个文件中展示所有相关代码
public class MainGUI {
private JLabel showLabel; // 声明为成员变量以便在ActionListener中访问
private CurrentTime currentTime; // 声明为成员变量
public MainGUI() {
currentTime = new CurrentTime();
}
public void showGUI(){
JFrame frame = new JFrame("目标倒计时");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setLocationRelativeTo(null); // 窗口居中显示
frame.setVisible(true);
JPanel jpanel = new JPanel();
// 初始化JLabel显示当前时间
String initialTime = currentTime.SystemDate();
showLabel = new JLabel(initialTime);
showLabel.setFont(new Font("宋体", Font.PLAIN, 20)); // 使用Font.PLAIN代替0
jpanel.add(showLabel, BorderLayout.PAGE_START); // 将标签放置在面板顶部
frame.add(jpanel);
// 创建并启动Swing Timer
// 参数1: 延迟时间 (毫秒),这里设置为1000毫秒,即1秒更新一次
// 参数2: ActionListener,每次Timer触发时执行其actionPerformed方法
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 在此方法中更新JLabel的文本
// 此方法总是在EDT上执行,因此更新UI是安全的
String updatedTime = currentTime.SystemDate();
showLabel.setText(updatedTime);
}
});
timer.start(); // 启动计时器
}
}
// CurrentTime类也作为内部类
public class CurrentTime {
public String SystemDate(){
LocalDateTime now_time = LocalDateTime.now();
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
return now_time.format(ofPattern);
}
}
}代码解析:
在Java Swing应用程序中实现实时时间显示或倒计时功能,关键在于正确处理UI更新的线程问题。javax.swing.Timer提供了一个优雅且线程安全的解决方案,它允许您在不阻塞EDT的情况下,周期性地在EDT上执行UI更新任务。通过理解其工作原理并遵循EDT规则,您可以构建响应迅速、用户体验良好的Swing应用程序。
以上就是Java Swing GUI中实现实时时间显示与倒计时教程的详细内容,更多请关注php中文网其它相关文章!
Windows激活工具是正版认证的激活工具,永久激活,一键解决windows许可证即将过期。可激活win7系统、win8.1系统、win10系统、win11系统。下载后先看完视频激活教程,再进行操作,100%激活成功。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号