Java实现动态数字时钟

 更新时间:2019年12月20日 15:10:55   作者:_yuanhao  
这篇文章主要为大家详细介绍了Java实现动态数字时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下

构建:

Clock继承 JFrame 为运行页面
ClockText 测试类 创建 Clock 对象 

运行效果:

具体实现:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 带你了解10道java入门面试题

    带你了解10道java入门面试题

    面试题相信大家都不陌生,想要一个好的工作面试题必不可少的,下面和小编一起来学习与了解Java当中有有些什么面试题吧,希望能给你带来帮助
    2021-08-08
  • Java结构型设计模式中建造者模式示例详解

    Java结构型设计模式中建造者模式示例详解

    建造者模式,是一种对象构建模式 它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现的对象。本文将通过示例讲解建造者模式,需要的可以参考一下
    2022-09-09
  • Java虚拟机JVM优化实战的过程全记录

    Java虚拟机JVM优化实战的过程全记录

    有人说Java之所以能够崛起,JVM功不可没。Java虚拟机最初服务于让Java语言凌驾于平台之上,实现“编写一次,到处运行”,那么下面这篇文章主要给大家分享了个关于Java虚拟机JVM优化实战的过程全记录,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • Java从数据库中读取Blob对象图片并显示的方法

    Java从数据库中读取Blob对象图片并显示的方法

    这篇文章主要介绍了Java从数据库中读取Blob对象图片并显示的方法,实例分析了Java读取数据库中Blob对象图片的技巧与操作方法,需要的朋友可以参考下
    2015-02-02
  • 解决Mybatis中mapper.xml文件update,delete及insert返回值问题

    解决Mybatis中mapper.xml文件update,delete及insert返回值问题

    这篇文章主要介绍了解决Mybatis中mapper.xml文件update,delete及insert返回值问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • mybatis 实现多层级collection嵌套

    mybatis 实现多层级collection嵌套

    这篇文章主要介绍了mybatis 实现多层级collection嵌套,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java使用WatchService监控文件内容变化的示例

    Java使用WatchService监控文件内容变化的示例

    本篇文章主要介绍了Java使用WatchService监控文件变化的示例,非常具有实用价值,需要的朋友可以参考下
    2017-10-10
  • 浅谈Spring Data Redis读不到设进去的值

    浅谈Spring Data Redis读不到设进去的值

    本文主要介绍了Spring Data Redis怎么读不到我刚才设进去的值,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Idea 2020.2 创建web、Spring项目的教程图解

    Idea 2020.2 创建web、Spring项目的教程图解

    这篇文章主要介绍了Idea 2020.2 创建web、Spring项目的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • JAVA十大排序算法之归并排序详解

    JAVA十大排序算法之归并排序详解

    这篇文章主要介绍了java中的归并排序,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08

最新评论