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();
}
}