java中GUI中显示当前时间_javaGUI界面实现动态时间显示——Swing中的计时器Timer

在Java中要实现时间的动态显示有不少种方法。下面就介绍其中一种简单的方法给你们。java

Swing中的计时器Timer,主要用到javax.swing.*包下的Timer类,该类能够周期的触发ActionEvent事件。ide

什么都不说,直接上代码:3d

private Timer time;

//时间显示

private JLabel getTimelabel() {

if (timelabel == null) {

timelabel = new JLabel("");

timelabel.setBounds(5, 65, 200, 20);

timelabel.setFont(new Font("微软雅黑", Font.BOLD, 12));

timelabel.setForeground(new Color(182, 229, 248));

time = new Timer(1000,new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

timelabel.setText(new SimpleDateFormat("yyyy年MM月dd日 EEEE hh:mm:ss").format(new Date()));

}

});

time.start();

}

return timelabel;

}

Timer能够经过start()方法来启动,stop()方法既是中止Timer。code

时间代码:orm

timelabel.setText(new SimpleDateFormat("yyyy年MM月dd日 EEEE hh:mm:ss").format(new Date()));

时间动态显示的主要代码:blog

time = new Timer(1000,new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

timelabel.setText(new SimpleDateFormat("yyyy年MM月dd日 EEEE hh:mm:ss").format(new Date()));

}

});

time.start();

最终在界面中的显示结果为:事件

d9cbda9f562f936f50d960d70c4b21ea.png

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java计时器实现示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TimerExample extends JFrame implements ActionListener { private JLabel timeLabel; private int seconds = 0; private int minutes = 0; private int hours = 0; public TimerExample() { // 设置窗口标题 super("计时器"); // 设置窗口大小 setSize(400, 200); // 设置窗口位置 setLocationRelativeTo(null); // 设置窗口关闭操作 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口布局 setLayout(new BorderLayout()); // 创建时间标签 timeLabel = new JLabel("00:00:00", SwingConstants.CENTER); timeLabel.setFont(new Font("宋体", Font.PLAIN, 60)); add(timeLabel, BorderLayout.CENTER); // 创建开始按钮 JButton startButton = new JButton("开始"); startButton.addActionListener(this); add(startButton, BorderLayout.WEST); // 创建停止按钮 JButton stopButton = new JButton("停止"); stopButton.addActionListener(this); add(stopButton, BorderLayout.EAST); // 显示窗口 setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("开始")) { // 创建计时器 Timer timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { // 更新时间 seconds++; if (seconds >= 60) { seconds = 0; minutes++; if (minutes >= 60) { minutes = 0; hours++; } } // 更新时间标签 timeLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds)); } }); // 启动计时器 timer.start(); } else if (command.equals("停止")) { // 停止计时器 seconds = 0; minutes = 0; hours = 0; timeLabel.setText("00:00:00"); } } public static void main(String[] args) { new TimerExample(); } } ``` 这个计时器使用了JavaSwing GUI库来创建窗口和组件。它包含一个时间标签、一个开始按钮和一个停止按钮。当点击开始按钮时,程序会创建一个计时器并开始计时,每隔一秒钟更新时间标签的显示。当点击停止按钮时,程序会停止计时器并将时间标签重置为"00:00:00"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值