Java动态显示当前日期和时间
Java动态显示当前系统的日期、时间;如图所示:
packagecom.xin.test;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjavax.swing.JLabel;
importjavax.swing.Timer;
importjavax.swing.JFrame;
publicclassNowTimeextendsJFrame{
privatestaticfinallongserialVersionUID=4306803332677233920L;
//添加显示时间的JLabel
publicNowTime(){
JLabeltime=newJLabel();
time.setForeground(Color.BLUE);
time.setBounds(30,0,900,130);
time.setFont(newFont("微软雅黑",Font.BOLD,80));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setTitle("nowtime");
this.setBounds(500,200,930,200);
this.setVisible(true);
this.add(time);
this.setTimer(time);
}
//设置Timer1000ms实现一次动作实际是一个线程
privatevoidsetTimer(JLabeltime){
finalJLabelvarTime=time;
TimertimeAction=newTimer(100,newActionListener(){
publicvoidactionPerformed(ActionEvente){
longtimemillis=System.currentTimeMillis();
//转换日期显示格式
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
varTime.setText(df.format(newDate(timemillis)));
}
});
timeAction.start();
}
//运行方法
publicstaticvoidmain(String[]args){
newNowTime();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。