java 整点执行_Java整点报时 | 学步园

本文展示了一个使用Java实现的整点报时程序,通过`Timer`类定时更新时间,并在分钟为0时进行整点报时,显示当前小时数。程序在窗口中显示时钟并持续5秒进行报时。
摘要由CSDN通过智能技术生成

import java.awt.*;

import java.util.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.Timer;

public class Test extends JFrame implements ActionListener {

Timer t = new Timer(1000, this);

GregorianCalendar calendar = new GregorianCalendar();

JLabel label1 = new JLabel(new Date().toString()), label2 = new JLabel(" ");

public Test() {

super("TIME");

Container c = this.getContentPane();

c.setLayout(new FlowLayout());

c.add(label1);

c.add(label2);

t.start();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setBounds(200, 200, 200, 100);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

Date date = new Date();

calendar.setTime(date);

if (e.getSource() == t) {

label1.setText(date.toString());

if (calendar.get(Calendar.MINUTE) == 0)// 当分钟为0报时

if (calendar.get(Calendar.SECOND) <= 5) {// 持续5秒报时

if (calendar.get(Calendar.SECOND) % 2 == 0)

label2.setText("现在是北京时间"+ calendar.get(Calendar.HOUR_OF_DAY) + "点整。");

if (calendar.get(Calendar.SECOND) % 2 == 1)

label2.setText(" ");

}

}

}

public static void main(String[] args) {

new Test();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值