Java如何实现动态数字时钟

发布时间:2021-04-14 14:03:30 作者:小新
来源:亿速云 阅读:269

小编给大家分享一下Java如何实现动态数字时钟,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

构建:

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

运行效果:

Java如何实现动态数字时钟

具体实现:

一、Clock类

具体实现如下:

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

以上是“Java如何实现动态数字时钟”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. js之数字时钟
  2. 使用Java怎么实现一个动态数字时钟功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java怎么实现图片加水印效果

下一篇:使用java实现航班信息查询管理系统的案例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》