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

  

时间: 2024-10-28 13:06:38

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

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

Java图形用户界面中,处理事件时所必须的步骤是: 1.创建接受响应的组件(控件)2.实现相关事件监听接口3.注册事件源的动作监听器4.事件触发时的事件处理 相应的可以通过以下的集中方式来作出事件响应. [java] view plaincopyprint? <span style="font-size: 18px;">一.容器类监听 效果:单击窗体中的三个按钮,实现相应的相应时间. </span><pre class="java" n

java中文乱码解决方法汇总

publicstaticvoidmain(String[]argv){ try{ System.out.println("中文");//1 System.out.println("中文".getBytes());//2 System.out.println("中文".getBytes("GB2312″));//3 System.out.println("中文".getBytes("ISO8859_1″));

Java实现文本文件分组汇总的简便方法

程序开发中经常会碰到处理文本文件中数据的情况,这里通过一个例子来看用java实现文本文件分组汇总的方法:从文本文件employee.txt中读取员工信息,按照DEPT分组,求出每组的员工个数COUNT和薪酬SALARY总额. 文本文件empolyee.txt的格式如下: EID   NAME       SURNAME        GENDER  STATE        BIRTHDAY        HIREDATE         DEPT         SALARY 1      

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

这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: 利用java.util.Timer和java.util.TimerTask来做动态更新,毕竟每次更新可以看作是计时1秒发生一次.代码如下: import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.C

Android项目:proguard混淆之常见问题及解决方法汇总

1.使用proguardgui混淆器对jar包进行混淆,出现EXCEPTION FROM SIMULATION错误: [2014-07-08 14:29:55 - Test024_HouseBox_v02_jar] Dx  EXCEPTION FROM SIMULATION: [2014-07-08 14:29:55 - Test024_HouseBox_v02_jar] Dx local variable type mismatch: attempt to set or access a va

Android学习笔记之SQLite数据库的使用及常用的增删改查方法、无sql语句的DRUD方法汇总

(1)目录结构如下: (2)链接数据库的文件:DBHelper.java要继承SQLiteOpenHelper类 package com.lc.sqlite_demo1.db; import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLit

内网端口转发方法汇总

内网端口转发方法汇总 I. Sample Baklinks with "lcx.exe" first download lcx.exe from attach.blackbap.org/down/yclj/lcx.exe the program only can running in Windows Server, the program can backlink 3389 to another server. Opening and Listening a Port(like 333

javascript刷新页面的方法汇总

如何实现刷新当前页面呢?借助js你将无所不能. 1,reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页.true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5("刷新") 2,replace 方法,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,你不能通过"前进

mahout推荐12-相似度方法汇总

将各个计算用户相似度的方法弄过来了,可以参考下.实际运行代码 数据文件 intro.csv内容: 直接复制就行了 1,101,5.01,102,3.01,103,2.5 2,101,2.02,102,2.52,103,5.02,104,2.0 3,101,2.53,104,4.03,105,4.53,107,5.0 4,101,5.04,103,3.04,104,4.54,106,4.0 5,101,4.05,102,3.05,103,2.05,104,4.05,105,3.55,106,4.0