1. HTML用java製作時鍾
給你一個 秒鍾能一秒一秒的跳動 的
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>數字時鍾</title>
</head>
<script language="JavaScript">
<!--
function showTime()
{
myTime=new Date();
var monthArray=new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
var dayArray =new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六","星期日");
year=myTime.getYear();
date=myTime.getDate();
hours=myTime.getHours();
minutes=myTime.getMinutes();
seconds=myTime.getSeconds();
suf="AM";
if(hours>12)
{
suf="PM";
hours=hours-12;
}
if(hours==0)
hours=12;
if(minutes<=9)
minutes="0"+minutes;
if(seconds<=9)
seconds="0"+seconds;
theTime="<font size=2>今天是:</font><br><font size=4>"+
year+"年"+monthArray[myTime.getMonth()]+date+"日"+""+dayArray[myTime.getDay()]+
"</font><br><font size=2>當前時間:</font>"+
"<br><font size=4 face=Arial>"+hours+":"+minutes+":"+seconds+""+suf+"</font>";
DT.innerHTML=theTime;
setTimeout("showTime()",1000);
}
-->
</script>
<body onload="showTime()">
<span id=DT style="position:absolute;left=35px;top=15px"></span>
</body>
</html>
2. JAVA 24小時 數字時鍾
如果我沒記錯的話,安裝JDK的時候有一個DEMO的目錄,裡面就有你要的這個程序的源文件
3. 怎麼用java編寫時鍾呀
import
java.util.*;
import
java.awt.*;
import
java.applet.*;
//impelements
Runnable
是線程程序的介面
public
class
Clock
extends
Applet
implements
Runnable
{
Thread
timer
=
null;
//
定義線程實體timer
int
xcenter
=
400,
ycenter
=
50;
int
Radius
=
ycenter
-
5;
public
void
init()
{
resize(400,
125);//
設置時鍾程序的窗口大小
setBackground(Color.white);//
設置小應用程序的背景色
}
public
void
paint(Graphics
g)
{
int
xh,
yh,
xm,
ym,
xs,
ys,
s,
m,
h;
String
today;
Date
dat
=
new
Date();
//
定義時間類dat
s
=
dat.getSeconds();
//
獲得時間秒
m
=
dat.getMinutes();
//
獲得時間分
h
=
dat.getHours();
today
=
dat.toLocaleString();
//
獲得字元串時間格式
g.clearRect(0,
0,
size().width,
size().height);
//
消除小應用程序
xcenter
=
xcenter
-
1;
//
向左移動一個像素點
if
(xcenter
<
-50)
xcenter
=
400;
//
如果xcenter小於-50,則回到初始位置
//
計算秒的坐標
xs
=
(int)
(Math.cos(s
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
5)
+
xcenter);
ys
=
(int)
(Math.sin(s
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
5)
+
ycenter);
//
計算分鍾的坐標
xm
=
(int)
(Math.cos(m
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
10)
+
xcenter);
ym
=
(int)
(Math.sin(m
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
10)
+
ycenter);
//
計算小時的坐標
xh
=
(int)
(Math.cos((h
*
30
+
m
/
2)
*
3.14f
/
180
-
3.14f
/
2)
*
(Radius
-
20)
+
xcenter);
yh
=
(int)
(Math.sin((h
*
30
+
m
/
2)
*
3.14f
/
180
-
3.14f
/
2)
*
(Radius
-
20)
+
ycenter);
g.setColor(Color.darkGray);
//
設置顏色
g.drawString("9",
xcenter
-
(Radius
-
5),
ycenter
+
3);
//
顯示時鍾上的數字『9』
g.drawString("3",
xcenter
+
(Radius
-
10),
ycenter
+
3);
//
顯示時鍾上的數字『3』
g.drawString("12",
xcenter
-
5,
ycenter
-
(Radius
-
13));
//
顯示時鍾上的數字'12'
g.drawString("6",
xcenter
-
3,
ycenter
+
(Radius
-
10));
//
顯示時鍾上的數字'6'
g.drawString(today,
0,
125);
//
顯示字元串時鍾
g.drawLine(xcenter,
ycenter,
xs,
ys);
//
畫秒針
g.setColor(Color.blue);
//
設置顏色
g.drawArc(xcenter
-
Radius,
ycenter
-
Radius,
2
*
Radius,
2
*
Radius,
0,
360);
//
畫鍾
g.drawLine(xcenter,
ycenter
-
1,
xm,
ym);
//
畫分針
g.drawLine(xcenter
-
1,
ycenter,
xm,
ym);
//
畫分針
g.drawLine(xcenter,
ycenter
-
1,
xh,
yh);
//
畫時針
g.drawLine(xcenter
-
1,
ycenter,
xh,
yh);
//
畫時針
}
public
void
start()
{
if
(timer
==
null)
{
timer
=
new
Thread(this);
//
生成Thread(多線程程序)的對象實體
timer.start();
//
啟動生成的線程
}
}
public
void
stop()
{
timer.stop();
//
停止線程的工作
timer
=
null;
//
放掉Thread對象
}
public
void
run()
//
改方法用來定義線程體,一旦線程被啟動執行,就開始執行這個方法
{
while
(timer
!=
null)
{
try
{
Thread.sleep(150);
//
使當前正在執行的線程進入睡眠時間由參數millis確定,
//
單位時間是毫秒,當這個時間過去,線程即可運行的
while
(timer
!=
null)
{
try
{
Thread.sleep(150);//
使用當前正在執行的線程進入睡眠時間由參數
//
millis確定,單位是毫秒,當這個時間過去,線程即為可運行的
}
catch
(InterruptedException
e)
{
}
repaint();
//
repaint所做的事其實是去調用方法uadate重畫效應用程序
}
timer
=
null;
}
catch
(InterruptedException
e)
{
}
}
}
//
所做的工作是先將整個效應用程序區域清除,再去調用paint,完成重畫的動作
public
void
update(Graphics
g)
{
paint(g);
}
}
4. 怎樣用java 程序寫一個時鍾程序
面向對象思想寫成:
下面是一個顯示器類
publicclassDisplay{
privateintvalue;//現在的值
privateintlimit;//上限值
Display(intlimit){
this.limit=limit;
}
publicvoidincrease(){
value++;
if(value==limit){
value=0;
}
}
publicintgetValue(){
returnvalue;
}
publicstaticvoidmain(String[]args){
Displayd=newDisplay(24);
for(;;){
d.increase();
System.out.println(d.getValue());
}
}
}
下面創建一個時鍾對象:
publicclassClock{
privateDisplayh=newDisplay(24);
privateDisplaymin=newDisplay(60);
privateDisplays=newDisplay(60);
publicvoidstart(){
for(;;){
s.increase();
if(s.getValue()==0){//如果分重置,小時+1
min.increase();
if(min.getValue()==0){//如果分重置,小時+1
h.increase();
}
}
System.out.printf("%02d:%02d:%02d ",h.getValue(),min.getValue(),s.getValue());//格式輸出
}
}
publicstaticvoidmain(String[]args){
Clockclock=newClock();
clock.start();
}
5. 基於Java的數字時鍾軟體設計
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.Timer;
public class test1 extends JFrame{
public test1(){
super();
setSize(300,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent evt) {
setTitle( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
);
timer.start();
}
public static void main( String... args ){
new test1();
}
}
6. Java 獲得純數字格式的時間
Java 獲得純數字格式的時間有下面三種方法
方法 一
System.currentTimeMillis();
方法 二
Calendar.getInstance().getTimeInMillis();
方法 三
new Date().getTime();
以下是效率對比:
import java.util.Calendar;
import java.util.Date;
public class TimeTest {
private static long _TEN_THOUSAND=10000;
public static void main(String[] args) {
long times=1000*_TEN_THOUSAND;
long t1=System.currentTimeMillis();
testSystem(times);
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
testCalander(times);
long t3=System.currentTimeMillis();
System.out.println(t3-t2);
testDate(times);
long t4=System.currentTimeMillis();
System.out.println(t4-t3);
}
public static void testSystem(long times){//use 188
for(int i=0;i<times;i++){
long currentTime=System.currentTimeMillis();
}
}
public static void testCalander(long times){//use 6299
for(int i=0;i<times;i++){
long currentTime=Calendar.getInstance().getTimeInMillis();
}
}
public static void testDate(long times){
for(int i=0;i<times;i++){
long currentTime=new Date().getTime();
}
}
}
因為很簡單我就不加註釋了,每種方法都運行1千萬次,然後查看運行結果
187
7032
297
結果發現 System.currentTimeMillis() 這種方式速度最快
Calendar.getInstance().getTimeInMillis() 這種方式速度最慢,看看源碼會發現,Canlendar因為要處理時區問題會耗費很多的時間。
所以建議多使用第一種方式。
7. 求一篇java程序:要求數字時鍾(19:59以及圓盤兩種表達方式)可修改時間。有個備忘錄
推薦鏈接 你自己看看http://wenku..com/link?url=-g_ACgmsFJjZMOBnWQf5_CnYD-wrbfXEpL7Q-x_
如果你連怎麼簡單的東西都需要別人給你做的完完整整你。你其實可以放棄java了。編程只能靠自學,沒能會逼你學的。這種時鍾的代碼 網上多的一比。自己好好想想!
8. 用JAVA寫數字時鍾
importjavax.swing.*;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.awt.*;
importjava.util.Calendar;
importjava.util.GregorianCalendar;
{
intx,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
finaldoubleRAD=Math.PI/180;
publicClock(){
super("Clockbykikiwawa");
setDefaultCloseOperation(3);
setSize(200,200);
setBackground(Color.BLACK);
setLocation(300,150);
setResizable(false);
setVisible(true);
intdelay=1000;
ActionListenerdrawClock=newActionListener(){
publicvoidactionPerformed(ActionEventevt){
repaint();
}
};
newTimer(delay,drawClock).start();
}
publicvoidactionPerformed(ActionEvente){
}
publicvoidpaint(Graphicsg){
Graphics2Dg2D=(Graphics2D)g;
Insetsinsets=getInsets();
intL=insets.left/2,T=insets.top/2;
h=getSize().height;
g.setColor(Color.white);
g2D.setStroke(newBasicStroke(4.0f));
g.drawOval(L+40,T+40,h-80,h-80);
r=h/2-40;
x0=40+r-5+L;
y0=40+r-5-T;
ang=60;
for(inti=1;i<=12;i++){
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
Calendarnow=newGregorianCalendar();
intnowh=now.get(Calendar.HOUR_OF_DAY);
intnowm=now.get(Calendar.MINUTE);
intnows=now.get(Calendar.SECOND);
Stringst;
if(nowh<10)st="0"+nowh;elsest=""+nowh;
if(nowm<10)st+=":0"+nowm;elsest+=":"+nowm;
if(nows<10)st+=":0"+nows;elsest+=":"+nows;
g.setColor(Color.pink);
g.fillRect(L,T,50,28);
g.setColor(Color.blue);
g.drawString(st,L+2,T+26);
ss=90-nows*6;
mm=90-nowm*6;
hh=90-nowh*30-nowm/2;
x0=r+40+L;
y0=r+40+T;
if(olds_x>0){
g.setColor(getBackground());
g.drawLine(x0,y0,olds_x,h-olds_y);
}
else{
old_m=mm;
old_h=hh;
}
x=(int)(r*0.9*Math.cos(RAD*ss))+x0;
y=(int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
g.setColor(Color.yellow);
g.drawLine(x0,y0,x,h-y);
olds_x=x;
olds_y=y;
g2D.setStroke(newBasicStroke(2.2f));
if(old_m!=mm){
g.setColor(getBackground());
g.drawLine(x0,y0,oldm_x,h-oldm_y);
}
x=(int)(r*0.7*Math.cos(RAD*mm))+x0;
y=(int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
g.setColor(Color.green);
g.drawLine(x0,y0,x,h-y);
oldm_x=x;
oldm_y=y;
old_m=mm;
g2D.setStroke(newBasicStroke(3.4f));
if(old_h!=hh){
g.setColor(getBackground());
g.drawLine(x0,y0,oldh_x,h-oldh_y);
}
x=(int)(r*0.5*Math.cos(RAD*hh))+x0;
y=(int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
g.setColor(Color.red);
g.drawLine(x0,y0,x,h-y);
oldh_x=x;
oldh_y=y;
old_h=hh;
}
publicstaticvoidmain(String[]args){
Clockc=newClock();
}}
4
9. java 數字時鍾 就是最簡單的當前時間~
給你個最精簡的swing。。。呵呵
importjava.awt.FlowLayout;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Locale;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
publicclassTest{
staticpublicvoidmain(String參數[]){
JFramef=newJFrame("時鍾示例");
f.setLayout(newFlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,50);
finalJLabell=newJLabel();
finalSimpleDateFormatformat=newSimpleDateFormat(
"y年M月d日EH:m:s",Locale.CHINA);
f.add(l);
Timert=newTimer();
t.scheleAtFixedRate(newTimerTask(){
@Override
publicvoidrun(){
l.setText(format.format(newDate(System.currentTimeMillis())));
}
},0,1000);
f.setVisible(true);
}
}
10. 用Java編寫小時鍾
貌似這個網上有JS插件的