java时钟_Java实现的简单数字时钟功能示例

本文实例讲述了Java实现的简单数字时钟功能。分享给大家供大家参考,具体如下:

应用名称:Java数字时钟

用到的知识:Java GUI编程,线程

开发环境:win8+eclipse+jdk1.8

功能说明:可以显示当前系统的年月日、星期以及准确时间,并实时更新显示。

效果图:

11594d577b11c1de7319905eb2cbea47.png

源代码:

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import java.awt.Font;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Time extends JFrame implements Runnable{

/**

*

*/

private static final long serialVersionUID = 1L;

private JLabel date;

private JLabel time;

public Time() {

//初始化图形界面

this.setVisible(true);

this.setTitle("数字时钟");

this.setSize(282, 176);

this.setLocation(200, 200);

this.setResizable(true);

JPanel panel = new JPanel();

getContentPane().add(panel, BorderLayout.CENTER);

panel.setLayout(null);

//时间

time = new JLabel();

time.setBounds(31, 54, 196, 59);

time.setFont(new Font("Arial", Font.PLAIN, 50));

panel.add(time);

//日期

date = new JLabel();

date.setFont(new Font("微软雅黑", Font.PLAIN, 13));

date.setBounds(47, 10, 180, 22);

panel.add(date);

}

//用一个线程来更新时间

public void run() {

while(true){

try{

date.setText(new SimpleDateFormat("yyyy 年 MM 月 dd 日 EEEE").format(new Date()));

time.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));

}catch(Throwable t){

t.printStackTrace();

}

}

}

public static void main(String[] args) {

new Thread(new Time()).start();

}

}

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

希望本文所述对大家java程序设计有所帮助。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 实现时钟效果可以使用 Java Swing 组件库中的 `JLabel` 和 `Timer` 组件。具体实现步骤如下: 1. 创建一个 `JFrame` 窗口,并添加一个 `JLabel` 组件用于显示当前时间。 2. 创建一个 `Timer` 组件,每隔一秒钟刷新一次时间。 3. 在 `Timer` 组件的 `ActionListener` 中更新 `JLabel` 组件的显示内容,即当前时间。 4. 使用 Java 的日期时间类 `java.time.LocalDateTime` 获取系统当前时间。 下面是一个简单Java 时钟实现示例: ```java import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Timer; public class Clock extends JFrame implements ActionListener { private JLabel timeLabel; private Timer timer; public Clock() { // 创建窗口 setTitle("Clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setSize(200, 100); // 创建时间标签 timeLabel = new JLabel(); timeLabel.setFont(new Font("Dialog", Font.PLAIN, 24)); add(timeLabel); // 创建定时器 timer = new Timer(1000, this); timer.start(); } @Override public void actionPerformed(ActionEvent e) { // 更新时间标签 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); timeLabel.setText(formatter.format(now)); } public static void main(String[] args) { Clock clock = new Clock(); clock.setVisible(true); } } ``` 这个时钟会在窗口中显示当前时间,并且每隔一秒钟自动更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值