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();
}
}
java实现时钟方法汇总的更多相关文章
- OpenCV3 Java 机器学习使用方法汇总
原文链接:OpenCV3 Java 机器学习使用方法汇总 前言 按道理来说,C++版本的OpenCV训练的版本XML文件,在java中可以无缝使用.但要注意OpenCV本身的版本问题.从2.4 到3 ...
- java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)
Java图形用户界面中,处理事件时所必须的步骤是: 1.创建接受响应的组件(控件)2.实现相关事件监听接口3.注册事件源的动作监听器4.事件触发时的事件处理 相应的可以通过以下的集中方式来作出事件响应 ...
- Java实现时间动态显示方法汇总
这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: ...
- java面试笔试大汇总
java面试笔试题大汇总5 JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象:2.继承:3.封装:4. 多态性: 2.String是最基本的数据类型吗? 基本数据类型包括byte.int. ...
- Linux下查看线程数的几种方法汇总
Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...
- 线程池(Java中有哪些方法获取多线程)
线程池(Java中有哪些方法获取多线程) 前言 获取多线程的方法,我们都知道有三种,还有一种是实现Callable接口 实现Runnable接口 实现Callable接口 实例化Thread类 使用线 ...
- 你真的会玩SQL吗?实用函数方法汇总
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- oracle调用JAVA类的方法
导入jar包 在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了, 1.操作系统需要拥有支持loadjava命令的jdk. 2.加 ...
- Java中的方法应用
一.如何定义java中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 语法: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.priv ...
随机推荐
- c# 处理js序列化时 datetime返回UTC格式的问题
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- HM visual studio编译报错
今天,编译HM的代码,发现编译报如下错误. error MSB6003: The specified task executable "cl.exe" could not be r ...
- Java之OO的特性与原则
OO(Object-Oriented)面向对象 面向对象,有三个特性.五个原则之说. 特性 封装 顾名思义,封装就好比一个黑盒子,内部被隐藏,只有几个接口与外界相连.具体来说,所谓封装 ...
- (转)3D LUT
这是 @Air君·Saunato·LoFoTo 大神运用3DLUT的作品 这是借用昔年大神 @昔年Olivia 用3DLUT的片片 http://paopaopaojiao.lofter.com/po ...
- Linux常用命令 (转载自大牛笔记 --- http://www.weixuehao.com)
Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Linux软件包管理 vim使用 用户及用户组管理 文件权限管理 大牛笔记-www.weix ...
- 日志组件logback的介绍及配置使用方法(一)
一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...
- XML 的 XPath 语法
XPath 是 XML 路径语言(XML Path Language),用来确定XML文档中某部分位置的语言.无论是什么语言什么框架,几乎都可以使用 XPath 来高效查询 XML 文件. 本文将介绍 ...
- python面试题(十)
Python中基本数据结构的操作 元组 列表 字典 集合 定义 新增 更改 删除 2.请尽可能列举python列表的成员方法,并给出一下列表操作的答案: (1)a=[1, 2, 3, 4, 5], a ...
- 使用反相器的rc振荡电路
多谐振荡器是一种自激振荡电路,该电路在接通电源后无需外接触发信号就能产生一定频率和幅值的矩形脉冲波或方波.由于多谐振荡器在工作过程中不存在稳定状态,故又称为无稳态电路. 一.门电路组成的多谐振荡器 1 ...
- PHP实现的多文件上传类及用法示例
这篇文章主要介绍了PHP实现的多文件上传类及用法,详细分析了php实现的多文件上传类与具体的使用技巧,需要的朋友可以参考下 1.upFiles.css.php 文件 <?php class Up ...