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

  

java实现时钟方法汇总的更多相关文章

  1. OpenCV3 Java 机器学习使用方法汇总

    原文链接:OpenCV3 Java 机器学习使用方法汇总  前言 按道理来说,C++版本的OpenCV训练的版本XML文件,在java中可以无缝使用.但要注意OpenCV本身的版本问题.从2.4 到3 ...

  2. java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)

    Java图形用户界面中,处理事件时所必须的步骤是: 1.创建接受响应的组件(控件)2.实现相关事件监听接口3.注册事件源的动作监听器4.事件触发时的事件处理 相应的可以通过以下的集中方式来作出事件响应 ...

  3. Java实现时间动态显示方法汇总

    这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: ...

  4. java面试笔试大汇总

    java面试笔试题大汇总5 JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象:2.继承:3.封装:4. 多态性: 2.String是最基本的数据类型吗? 基本数据类型包括byte.int. ...

  5. Linux下查看线程数的几种方法汇总

    Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...

  6. 线程池(Java中有哪些方法获取多线程)

    线程池(Java中有哪些方法获取多线程) 前言 获取多线程的方法,我们都知道有三种,还有一种是实现Callable接口 实现Runnable接口 实现Callable接口 实例化Thread类 使用线 ...

  7. 你真的会玩SQL吗?实用函数方法汇总

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  8. oracle调用JAVA类的方法

    导入jar包 在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了,  1.操作系统需要拥有支持loadjava命令的jdk.  2.加 ...

  9. Java中的方法应用

    一.如何定义java中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 语法: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.priv ...

随机推荐

  1. c# 处理js序列化时 datetime返回UTC格式的问题

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  2. mysql连接踩坑

    本机安装的是wamp,集成了mysql.php.apache.安装了sqlyog客户端. 1.错误代码2003 证明mysql服务没有开启,此时需要开启mysql服务,开启了wamp 2.错误代码10 ...

  3. 使用stringstream类

    当需要格式化int类型为字符串时,可以使用这个类, 需要包含这个文件头: #include <sstream> 然后这样使用: //打开保存进度的RPG文件. std::stringstr ...

  4. 我也说说Emacs吧(2) - Emacs其实就是函数的组合

    Emacs本质上是函数的组合 从帮助上看emacs有何不同 Vim和Sublime Text等编辑器,本质上是一个编辑器. 比如我们看看vim的帮助,是这个风格的,比如我要看i命令的帮助: <i ...

  5. js 以函数名作为参数动态执行 函数

    function myFunc() { console.log(11111); } test("myFunc"); function test(funcName) { if(typ ...

  6. 【Keras学习】资源

    Keras项目github源码(python):keras-team/keras: Deep Learning for humans 里面的docs包含说明文档 中文文档:Keras中文文档 预训练模 ...

  7. pandas.read_csv()参数(转载)

    文章转载地址 pandas.read_csv参数整理 读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/p ...

  8. Percona Xtrabackup 安装

    1.安装Percona Xtrabackup YUM Repository --安装repository [root@manager ~]# yum install https://www.perco ...

  9. linux下vi命令(转)

    进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...

  10. IE中拖放问题

    1.所有的元素上都能绑定放置目标的事件,但并不保证此元素是有效的放置对象. 默认情况下,IE中只有文本框(<input/>或<textarea/>)是网页上唯一有效的放置目标. ...