java程序 时钟 整点报时代码
发布网友
发布时间:2022-04-27 07:53
我来回答
共3个回答
热心网友
时间:2023-09-14 02:45
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();
}
}
热心网友
时间:2023-09-14 02:45
没有的如果有我早就用了
热心网友
时间:2023-09-14 02:46
莫名其妙