Java实现动态数字时钟
本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下
构建:
Clock继承JFrame为运行页面
ClockText测试类创建Clock对象
运行效果:
具体实现:
一、Clock类
- 四个JPnal三个放时间最后一个放日期
- 放时间的三个JPnal分别加入地点时间按钮
- 最后一个按钮添加日期
具体实现如下:
publicclassClockextendsJFrame{
privateJPaneljPanelBeijing;
privateJPaneljPanelNewYork;
privateJPaneljPanelLondom;
privateJPaneljPanelDate;
privatebooleanBeijingThreadFlag_IsStart=true;
privatebooleanNewYorkThreadFlag_IsStart=true;
privatebooleanLondonThreadFlag_IsStart=true;
publicClock(){
//TODOAuto-generatedconstructorstub
jPanelBeijing=newJPanel();
jPanelNewYork=newJPanel();
jPanelLondom=newJPanel();
jPanelDate=newJPanel();
iniRelations();
iniLayout();
jFrameClick();
setVisible(true);
setSize(480,225);
setLocationRelativeTo(null);
}
privatevoidiniLayout(){
jPanelBeijing.setLayout(newGridLayout(3,1));
jPanelNewYork.setLayout(newGridLayout(3,1));
jPanelLondom.setLayout(newGridLayout(3,1));
}
//关系
privatevoidiniRelations(){
this.add(BorderLayout.WEST,jPanelBeijing);
this.add(BorderLayout.CENTER,jPanelNewYork);
this.add(BorderLayout.EAST,jPanelLondom);
this.add(BorderLayout.SOUTH,jPanelDate);
FontplaceFont=newFont("楷体",Font.BOLD,36);
JLabeljLabelBeijing=newJLabel("北京时间");
jLabelBeijing.setFont(placeFont);
jPanelBeijing.add(jLabelBeijing);
setWestPanel();
JLabeljLabelNewYork=newJLabel("纽约时间");
jLabelNewYork.setFont(placeFont);
jPanelNewYork.add(jLabelNewYork);
setCenterPanel();
JLabeljLabelLondon=newJLabel("伦敦时间");
jLabelLondon.setFont(placeFont);
jPanelLondom.add(jLabelLondon);
setEastPanel();
setDatePanel();
}
privatevoidsetWestPanel(){
//addtimeforSouthPanel
JLabeljLabelTime=newJLabel("加载中.");
jLabelTime.setFont(newFont("宋体",Font.BOLD,30));
TimertimeAction=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
longtimemillis=System.currentTimeMillis();
//转换日期显示格式
SimpleDateFormattime=newSimpleDateFormat("HH:mm:ss");
jLabelTime.setText(time.format(newDate(timemillis)));
}
});
timeAction.start();
jPanelBeijing.add(jLabelTime);
Buttonbutton=newButton("北京暂停");
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(BeijingThreadFlag_IsStart){
timeAction.stop();
button.setLabel("北京继续");
BeijingThreadFlag_IsStart=false;
}else{
timeAction.start();
button.setLabel("北京暂停");
BeijingThreadFlag_IsStart=true;
}
}
});
jPanelBeijing.add(button);
}
privatevoidsetCenterPanel(){
//addtimeforSouthPanel
JLabeljLabelTime=newJLabel("加载中.");
jLabelTime.setFont(newFont("宋体",Font.BOLD,30));
TimertimeAction=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
longtimemillis=System.currentTimeMillis();
//转换日期显示格式
SimpleDateFormattime=newSimpleDateFormat("HH:mm:ss");
jLabelTime.setText(time.format(newDate(timemillis-13*60*60*1000)));
}
});
timeAction.start();
jPanelNewYork.add(jLabelTime);
Buttonbutton=newButton("纽约暂停");
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(NewYorkThreadFlag_IsStart){
timeAction.stop();
button.setLabel("纽约继续");
NewYorkThreadFlag_IsStart=false;
}else{
timeAction.start();
button.setLabel("纽约暂停");
NewYorkThreadFlag_IsStart=true;
}
}
});
jPanelNewYork.add(button);
}
privatevoidsetEastPanel(){
//addtimeforSouthPanel
//JLabeljLabelDate=newJLabel("Date");
JLabeljLabelTime=newJLabel("加载中.");
jLabelTime.setFont(newFont("宋体",Font.BOLD,30));
TimertimeAction=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
longtimemillis=System.currentTimeMillis();
//SimpleDateFormatdate=newSimpleDateFormat("yyyy年MM月dd
//日");
//jLabelDate.setText("当前日期:"+date.format(new
//Date(timemillis)));
SimpleDateFormattime=newSimpleDateFormat("HH:mm:ss");
jLabelTime.setText(time.format(newTime(timemillis-8*60*60*1000)));
}
});
timeAction.start();
jPanelLondom.add(jLabelTime);
Buttonbutton=newButton("伦敦暂停");
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(LondonThreadFlag_IsStart){
timeAction.stop();
button.setLabel("伦敦继续");
LondonThreadFlag_IsStart=false;
}else{
timeAction.start();
button.setLabel("伦敦暂停");
LondonThreadFlag_IsStart=true;
}
}
});
jPanelLondom.add(button);
//jPanelLondom.add(jLabelDate);
}
privatevoidsetDatePanel(){
//addtimeforSouthPanel
JLabeljLabelDate=newJLabel("加载中.");
TimertimeAction=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
longtimemillis=System.currentTimeMillis();
SimpleDateFormatdate=newSimpleDateFormat("yyyy年MM月dd日");
jLabelDate.setText("当前日期:"+date.format(newDate(timemillis)));
}
});
timeAction.start();
jPanelDate.add(jLabelDate);
}
privatevoidjFrameClick(){
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//设置不默认关闭
addWindowListener(newWindowListener(){
@Override
publicvoidwindowOpened(WindowEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidwindowIconified(WindowEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidwindowDeiconified(WindowEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidwindowDeactivated(WindowEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidwindowClosing(WindowEvente){
//TODOAuto-generatedmethodstub
intx=JOptionPane.showConfirmDialog(null,"确认退出么?","友情提示",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if(x==0){
System.exit(0);
}
}
@Override
publicvoidwindowClosed(WindowEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidwindowActivated(WindowEvente){
//TODOAuto-generatedmethodstub
}
});
}
}
二、创建ClockText类用于测试
publicclassClockText{
publicstaticvoidmain(String[]args){
newClock();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。