欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现的动态数字时钟功能示例【显示世界时间】

程序员文章站 2024-03-31 13:12:34
本文实例讲述了java实现的动态数字时钟功能。分享给大家供大家参考,具体如下: 构建: 1. clock继承 jframe 为运行页面 2. clocktext 测试...

本文实例讲述了java实现的动态数字时钟功能。分享给大家供大家参考,具体如下:

构建:

1. clock继承 jframe 为运行页面
2. clocktext 测试类 创建 clock 对象

运行效果:

Java实现的动态数字时钟功能示例【显示世界时间】

具体实现:

一、clock类 

  • 四个jpnal 三个放时间 最后一个放日期
  • 放时间的三个jpnal 分别加入 地点 时间 按钮
  • 最后一个按钮添加日期

具体实现如下:

public class clock extends jframe {
    private jpanel jpanelbeijing;
    private jpanel jpanelnewyork;
    private jpanel jpanellondom;
    private jpanel jpaneldate;
    private boolean beijingthreadflag_isstart = true;
    private boolean newyorkthreadflag_isstart = true;
    private boolean londonthreadflag_isstart = true;
    public clock() {
        // todo auto-generated constructor stub
        jpanelbeijing = new jpanel();
        jpanelnewyork = new jpanel();
        jpanellondom = new jpanel();
        jpaneldate = new jpanel();
        inirelations();
        inilayout();
        jframeclick();
        setvisible(true);
        setsize(480, 225);
        setlocationrelativeto(null);
    }
    private void inilayout() {
        jpanelbeijing.setlayout(new gridlayout(3, 1));
        jpanelnewyork.setlayout(new gridlayout(3, 1));
        jpanellondom.setlayout(new gridlayout(3, 1));
    }
    // 关系
    private void inirelations() {
        this.add(borderlayout.west, jpanelbeijing);
        this.add(borderlayout.center, jpanelnewyork);
        this.add(borderlayout.east, jpanellondom);
        this.add(borderlayout.south, jpaneldate);
        font placefont = new font("楷体", font.bold, 36);
        jlabel jlabelbeijing = new jlabel("北京时间");
        jlabelbeijing.setfont(placefont);
        jpanelbeijing.add(jlabelbeijing);
        setwestpanel();
        jlabel jlabelnewyork = new jlabel("纽约时间");
        jlabelnewyork.setfont(placefont);
        jpanelnewyork.add(jlabelnewyork);
        setcenterpanel();
        jlabel jlabellondon = new jlabel("伦敦时间");
        jlabellondon.setfont(placefont);
        jpanellondom.add(jlabellondon);
        seteastpanel();
        setdatepanel();
    }
    private void setwestpanel() {
        // add time for southpanel
        jlabel jlabeltime = new jlabel("加载中.");
        jlabeltime.setfont(new font("宋体", font.bold, 30));
        timer timeaction = new timer(1000, new actionlistener() {
            public void actionperformed(actionevent e) {
                long timemillis = system.currenttimemillis();
                // 转换日期显示格式
                simpledateformat time = new simpledateformat("hh:mm:ss ");
                jlabeltime.settext(time.format(new date(timemillis)));
            }
        });
        timeaction.start();
        jpanelbeijing.add(jlabeltime);
        button button = new button("北京暂停");
        button.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {
                // todo auto-generated method stub
                if (beijingthreadflag_isstart) {
                    timeaction.stop();
                    button.setlabel("北京继续");
                    beijingthreadflag_isstart = false;
                } else {
                    timeaction.start();
                    button.setlabel("北京暂停");
                    beijingthreadflag_isstart = true ;
                }
            }
        });
        jpanelbeijing.add(button);
    }
    private void setcenterpanel() {
        // add time for southpanel
        jlabel jlabeltime = new jlabel("加载中.");
        jlabeltime.setfont(new font("宋体", font.bold, 30));
        timer timeaction = new timer(1000, new actionlistener() {
            public void actionperformed(actionevent e) {
                long timemillis = system.currenttimemillis();
                // 转换日期显示格式
                simpledateformat time = new simpledateformat("hh:mm:ss ");
                jlabeltime.settext(time.format(new date(timemillis - 13 * 60 * 60 * 1000)));
            }
        });
        timeaction.start();
        jpanelnewyork.add(jlabeltime);
        button button = new button("纽约暂停");
        button.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {
                // todo auto-generated method stub
                if (newyorkthreadflag_isstart) {
                    timeaction.stop();
                    button.setlabel("纽约继续");
                    newyorkthreadflag_isstart = false;
                } else {
                    timeaction.start();
                    button.setlabel("纽约暂停");
                    newyorkthreadflag_isstart = true ;
                }
            }
        });
        jpanelnewyork.add(button);
    }
    private void seteastpanel() {
        // add time for southpanel
        // jlabel jlabeldate = new jlabel("date");
        jlabel jlabeltime = new jlabel("加载中.");
        jlabeltime.setfont(new font("宋体", font.bold, 30));
        timer timeaction = new timer(1000, new actionlistener() {
            public void actionperformed(actionevent e) {
                long timemillis = system.currenttimemillis();
                // simpledateformat date = new simpledateformat("yyyy 年 mm 月 dd
                // 日 ");
                // jlabeldate.settext(" 当前日期: " + date.format(new
                // date(timemillis)));
                simpledateformat time = new simpledateformat("hh:mm:ss ");
                jlabeltime.settext(time.format(new time(timemillis - 8 * 60 * 60 * 1000)));
            }
        });
        timeaction.start();
        jpanellondom.add(jlabeltime);
        button button = new button("伦敦暂停");
        button.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {
                // todo auto-generated method stub
                if (londonthreadflag_isstart) {
                    timeaction.stop();
                    button.setlabel("伦敦继续");
                    londonthreadflag_isstart = false;
                } else {
                    timeaction.start();
                    button.setlabel("伦敦暂停");
                    londonthreadflag_isstart = true ;
                }
            }
        });
        jpanellondom.add(button);
        // jpanellondom.add(jlabeldate);
    }
    private void setdatepanel() {
        // add time for southpanel
        jlabel jlabeldate = new jlabel("加载中.");
        timer timeaction = new timer(1000, new actionlistener() {
            public void actionperformed(actionevent e) {
                long timemillis = system.currenttimemillis();
                 simpledateformat date = new simpledateformat("yyyy 年 mm 月 dd 日 ");
                 jlabeldate.settext(" 当前日期: " + date.format(new date(timemillis)));
             }
        });
        timeaction.start();
        jpaneldate.add(jlabeldate);
    }
    private void jframeclick(){
        setdefaultcloseoperation(jframe.do_nothing_on_close);//设置不默认关闭
        addwindowlistener(new windowlistener() {
            @override
            public void windowopened(windowevent e) {
                // todo auto-generated method stub
            }
            @override
            public void windowiconified(windowevent e) {
                // todo auto-generated method stub
            }
            @override
            public void windowdeiconified(windowevent e) {
                // todo auto-generated method stub
            }
            @override
            public void windowdeactivated(windowevent e) {
                // todo auto-generated method stub
            }
            @override
            public void windowclosing(windowevent e) {
                // todo auto-generated method stub
                int x = joptionpane.showconfirmdialog(null, "确认退出么?", "友情提示", joptionpane.ok_cancel_option,
                        joptionpane.warning_message);
                if (x == 0) {
                    system.exit(0);
                }
            }
            @override
            public void windowclosed(windowevent e) {
                // todo auto-generated method stub
            }
            @override
            public void windowactivated(windowevent e) {
                // todo auto-generated method stub
            }
        });
    }
}

二、创建clocktext类用于测试

public class clocktext{
    public static void main(string[] args) {
        new clock();
    }
}

ps:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线世界各地时间查询:

unix时间戳(timestamp)转换工具:

在线日期/天数计算器:

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。