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 时钟 算法分析_java实现时钟方法汇总相关推荐

  1. java常量定义方法_Java常量定义方法汇总

    Java常量定义方法汇总 时间:2017-06-13     来源:华清远见JAVA学院 实际工作开发中,我们经常会使用到常量.那么Java常量如何定义呢?Java常量定义的规范是什么?定义Java常 ...

  2. java secretkey用法_Java SecretKeyFactory.generateSecret方法代码示例

    本文整理汇总了Java中javax.crypto.SecretKeyFactory.generateSecret方法的典型用法代码示例.如果您正苦于以下问题:Java SecretKeyFactory ...

  3. java set 包含_Java Set.contains()方法:判断Set集合是否包含指定的对象

    Java 集合类中的 Set.contains() 方法判断 Set 集合是否包含指定的对象.该方法返回值为 boolean 类型,如果 Set 集合包含指定的对象,则返回 true,否则返回 fal ...

  4. java 发送邮件 菜鸟_Java发送邮件的方法

    1.需要的jar包 2.具体实现方法 1.设置邮箱主机.需要认证.邮箱协议 Properties pro=new Properties(); pro.setProperty("mail.ho ...

  5. java super用法_Java基础面试题汇总

    blog.csdn.net/ThinkWon/article/details/104390612 Java概述 何为编程 编程就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码,并最终得到结 ...

  6. java 虚函数_Java的虚方法

    虚方法出现在Java的多态特性中, 父类与子类之间的多态性,对父类的函数进行重新定义.如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding).在Java中,子类 ...

  7. java post 发送_Java发送post方法详解

    总结一下java使用http发送post的方法: 1.post请求用于发送json 格式的参数: /** * post请求(用于请求json格式的参数) * * @param url 地址 * @pa ...

  8. java printwriter追加_Java PrintWriter append()方法

    Java PrintWriter append()方法 java.io.PrintWriter.append(char c) 方法将指定字符到此Writer. 1 语法 public PrintWri ...

  9. java 模拟时钟程序_java模拟时钟程序

    } clock_revise(); } final_print_out(); //时钟修正 //仿真结果打印输出 下图表示了 GPSS/JAVA 的运行逻辑初始化 程序 1. 设定仿真开始时间 2.初 ...

最新文章

  1. TricycleGAN:基于形状先验的无监督图像合成和分割
  2. Java截取最后一个/后面的所有字符
  3. linux platform匹配机制,Linux驱动中的platform总线详解
  4. [BUUCTF-pwn]——cmcc_simplerop (ropchain)
  5. CodeChef March Lunchtime 2018 div2
  6. 幼儿园 c语言,【资源学习】c语言程序代码,登录幼儿园200个小朋友的数据
  7. stm32f103r8t6的晶振频率_STM32F103R8T6[1]
  8. JavaScript字符串替换replace方法
  9. 使用telnet登录varnish3.x管理缓存时认证问题
  10. 诚龙网刻报错_诚龙网刻图文教程
  11. 微软雅黑字体的设计理念
  12. azw3怎么在iphone上打开?
  13. obs多推流地址_OBS直播进阶操作使用手册
  14. 基于Token的验证方式(JWT简笔)
  15. 怎么定位门面位置_如何选择店面位置
  16. SVM训练莺尾花数据集
  17. 解读正则化 LASSO回归 岭回归
  18. 轮播图的两种方法及自动轮播
  19. 可使用 git 操作的数据库 dolt
  20. windows远程登录应用

热门文章

  1. GuGuFishtion(2018 Multi-University Training Contest 7)
  2. F:Maximum White Subtree(树形dp)
  3. CF773F Test Data Generation(倍增FFT/动态规划)
  4. Harbour.Space Scholarship Contest 2021-2022 F. Pairwise Modulo 逆向思维 + 树状数组
  5. Codeforces Round #225 (Div. 1) E. Vowels 容斥 + sosdp
  6. 【牛客NOIP模拟】牛半仙的魔塔(增强版)【贪心】【并查集】
  7. P2634 [国家集训队]聪聪可可(树上启发式合并)
  8. [SDOI2008]SUE的小球
  9. 【平衡规划】Arithmetic Operations(CF1654E)
  10. 【模拟】【递归】电子表格(jzoj 2127)