java 时钟 算法分析_java实现时钟方法汇总

import java.awt.Dimension;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

//第一种比较推荐:

public class TimeFrame extends JFrame

{

/*

* Variables

*/

private JPanel timePanel;

private JLabel timeLabel;

private JLabel displayArea;

private String DEFAULT_TIME_FORMAT = "HH:mm:ss";

private String time;

private int ONE_SECOND = 1000;

public TimeFrame()

{

timePanel = new JPanel();

timeLabel = new JLabel("CurrentTime: ");

displayArea = new JLabel();

configTimeArea();

timePanel.add(timeLabel);

timePanel.add(displayArea);

this.add(timePanel);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(new Dimension(200,70));

this.setLocationRelativeTo(null);

}

/**

* This method creates a timer task to update the time per second

*/

private void configTimeArea() {

Timer tmr = new Timer();

tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);

}

/**

* Timer task to update the time display area

*

*/

protected class JLabelTimerTask extends TimerTask{

SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

@Override

public void run() {

time = dateFormatter.format(Calendar.getInstance().getTime());

displayArea.setText(time);

}

}

public static void main(String arg[])

{

TimeFrame timeFrame=new TimeFrame();

timeFrame.setVisible(true);

}

}

import java.awt.Dimension;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

//第二种

public class DTimeFrame2 extends JFrame implements Runnable{

private JFrame frame;

private JPanel timePanel;

private JLabel timeLabel;

private JLabel displayArea;

private String DEFAULT_TIME_FORMAT = "HH:mm:ss";

private int ONE_SECOND = 1000;

public DTimeFrame2()

{

timePanel = new JPanel();

timeLabel = new JLabel("CurrentTime: ");

displayArea = new JLabel();

timePanel.add(timeLabel);

timePanel.add(displayArea);

this.add(timePanel);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(new Dimension(200,70));

this.setLocationRelativeTo(null);

}

public void run()

{

while(true)

{

SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

displayArea.setText(dateFormatter.format(

Calendar.getInstance().getTime()));

try

{

Thread.sleep(ONE_SECOND);

}

catch(Exception e)

{

displayArea.setText("Error!!!");

}

}

}

public static void main(String arg[])

{

DTimeFrame2 df2=new DTimeFrame2();

df2.setVisible(true);

Thread thread1=new Thread(df2);

thread1.start();

}

}

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

//第三种:多国时钟实现

public class WorldTimeFrame extends JFrame

{

/**

*

*/

private static final long serialVersionUID = 4782486524987801209L;

private String time;

private JPanel timePanel;

private TimeZone timeZone;//时区

private JComboBox zoneBox;

private JLabel displayArea;

private int ONE_SECOND = 1000;

private String DEFAULT_FORMAT = "EEE d MMM, HH:mm:ss";

public WorldTimeFrame()

{

zoneBox = new JComboBox();

timePanel = new JPanel();

displayArea = new JLabel();

timeZone = TimeZone.getDefault();

zoneBox.setModel(new DefaultComboBoxModel(TimeZone.getAvailableIDs()));

zoneBox.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

updateTimeZone(TimeZone.getTimeZone((String) zoneBox.getSelectedItem()));

}

});

configTimeArea();

timePanel.add(displayArea);

this.setLayout(new BorderLayout());

this.add(zoneBox, BorderLayout.NORTH);

this.add(timePanel, BorderLayout.CENTER);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

pack();

}

/**

* This method creates a timer task to update the time per second

*/

private void configTimeArea() {

Timer tmr = new Timer();

tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);

}

/**

* Timer task to update the time display area

*

*/

public class JLabelTimerTask extends TimerTask{

SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_FORMAT, Locale.ENGLISH);

@Override

public void run() {

dateFormatter.setTimeZone(timeZone);

time = dateFormatter.format(Calendar.getInstance().getTime());

displayArea.setText(time);

}

}

/**

* Update the timeZone

* @param newZone

*/

public void updateTimeZone(TimeZone newZone)

{

this.timeZone = newZone;

}

public static void main(String arg[])

{

new WorldTimeFrame();

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/327746.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java IO: 网络

转载自 Java IO: 网络译文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) 校对:方腾飞 Java中网络的内容或多或少的超出了Java IO的范畴。关于Java网络更多的是在我的Java网络教程中探讨。但是既然网络是一个常见的数据来源以及数据流目的地&#xf…

配置phython环境

参考资料 https://www.runoob.com/python/python-install.html https://www.cnblogs.com/huangbiquan/p/7784533.html Python下载 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:https://www.py…

ASP.NET Core 之 Identity 入门(三)

前言 在上一篇文章中,我们学习了 CookieAuthentication 中间件,本篇的话主要看一下 Identity 本身。 最早2005年 ASP.NET 2.0 的时候开始, Web 应用程序在处理身份验证和授权有了很多的变化,多了比如手机端,平板等&…

玩物得志Java笔试题_代码规范利器-CheckStyle

本期内容分为五个部分,阅读时长预估7分钟:使用背景CheckStyle使用意义CheckStyle安装与使用CheckStyle检查配置示例落地使用情况及效果使用背景玩物得志目前还处在一个狂奔业务的时期,开发一般都全力支撑业务的快速奔跑,没有太多的…

Json交互处理

Json交互处理 JSON简介 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛。采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写&#xff0…

基于Spring boot,使用idea方便地切换启动环境

https://blog.csdn.net/mate_ge/article/details/78624579 基于Spring boot,使用idea方便地切换启动环境 原创martsforever 最后发布于2017-11-24 14:49:30 阅读数 17615 收藏 展开 在真实项目开发的时候,一定会有多个环境,这里以开发环境和…

Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)

package cn.bdqn.mhouse.entity; /*** * * 项目名称:house * 类名称:HouseCondition * 类描述: 动态查询房屋信息的条件类 * 创建人:Mu Xiongxiong * 创建时间:2017-3-10 下午9:39:21 * 修改人&…

Java IO: 字节和字符数组

转载自 Java IO: 字节和字符数组译文链接 作者: Jakob Jenkov 译者:homesick 内容列表 从InputStream或者Reader中读入数组从OutputStream或者Writer中写数组 在java中常用字节和字符数组在应用中临时存储数据。而这些数组又是通常的数据读取来源或…

利用 async amp; await 的异步编程

一、异步编程的简介 通过使用异步编程,你可以避免性能瓶颈并增强应用程序的总体响应能力。 Visual Studio 2012 引入了一个简化的方法,异步编程,在 .NET Framework 4.5 和 Windows 运行时利用异步支持。编译器可执行开发人员曾进行的高难度工…

1分钟学会python_快速入门:十分钟学会Python

类Python支持有限的多继承形式。私有变量和方法可以通过添加至少两个前导下划线和最多尾随一个下划线的形式进行声明(如“__spam”,这只是惯例,而不是Python的强制要求)。当然,我们也可以给类的实例取任意名称。例如:然&#xff0…

xml配置文件显示为文本文件问题

idea 新建的xml文件显示为文本问题 原因: 由于新建不带后缀名的文件的时候 idea会相对智能的让你选择 文件规则 解决: settings->File types 中找到对应的文件类型显示 ,把 你不小心添加的 正则 给去除就好了, 我这里的配置如下图 可以自己进行设置(&…

Java IO: System.in, System.out, System.err

转载自 Java IO: System.in, System.out, System.err译文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) System.in, System.out, System.err这3个流同样是常见的数据来源和数据流目的地。使用最多的可能是在控制台程序里利用System.out将输出打印到控制台上。 …

.NET应用迁移到.NET Core--调查案例

上周已经发过三篇文章讲述做.NET 应用迁移到.NET Core的一般方法,具体内容请看: .NET应用迁移到.NET Core(一) .NET应用迁移到.NET Core(二)风险评估 .NET应用迁移到.NET Core(三)从…

Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)

package cn.bdqn.mhouse.dao;import java.util.List;import cn.bdqn.mhouse.entity.House; import cn.bdqn.mhouse.entity.HouseCondition; import cn.bdqn.mhouse.util.Page; /*** * * 项目名称:mhouse * 类名称:IHouseDao * 类描述&#xf…

php curl post 超时设置,在PHP中设置curl的超时参数(timeout)

如下:我通过php在一个已经建好的数据库上发起curl请求.这个数据库非常庞大,因此它始终需要很长时间返回XML响应.为了解决这个问题,我准备了一个应该有长超时时间的curl请求.$ch curl_init();$headers["Content-Length"] strlen($postString);$headers["User-A…

Java IO: 流

转载自 Java IO: 流原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) Java IO流是既可以从中读取,也可以写入到其中的数据流。正如这个系列教程之前提到过的,流通常会与数据源、数据流向目的地相关联,比如文件、网络等等。 …

Ajax前后端对接---Springmvc

Springmvc实现 实体类user Data AllArgsConstructor NoArgsConstructor public class User {private String name;private int age;private String sex;}我们来获取一个集合对象&#xff0c;展示到前端页面 RequestMapping("/a2") public List<User> ajax2(…

缓存在大型网站架构中的应用

缓存的基本知识 在整个计算机体系构造中&#xff08;无论是硬件层面还是软件层面&#xff09;&#xff0c;缓存都是无处不在的。 在计算机硬件构造中&#xff0c;由于两种介质的速度不匹配&#xff0c;高速介质在和低速介质交互时速度趋向低速方&#xff0c;这就导致了高速介质…

php 错误提示开启,php开启与关闭错误提示,php开启错误提示_PHP教程

php开启与关闭错误提示&#xff0c;php开启错误提示windows系统开关php错误提示如果不具备修改php.ini的权限&#xff0c;可以将如下代码加入php文件中&#xff1a;代码如下 复制代码ini_set(“display_errors”, “On”);error_reporting(E_ALL | E_STRICT);当然&#xff0c;如…