導航:首頁 > 編程語言 > java時鍾代碼

java時鍾代碼

發布時間:2022-11-16 07:26:55

⑴ 如何用java編寫帶時鍾的萬年歷代碼,萬年歷要有陰歷演算法

如圖:

來源:http://www.javaeye.com/topic/735220

如調試不成功,我把源碼發你信箱里

⑵ 編寫Java應用程序。首先,定義一個時鍾類——Clock,

class clock { public: clock(); ~clock(); SetAlarm(int hour,int minute,int second); increamenthour(); increamentminute(); increamentsecond(); run(int hour,int minute,int second); private: int hr; int min; int sec; } void clock::SetAlarm(int hour,int minute,int second) { if(0<=hours&&hours<24) hr=hour; else hr=0; if(0<=hours&&hours<24) min=minute; else min=0; if(0<=hours&&hours<24) sec=second; else sec=0; } void clock::increamenthour() { hr++; if(hr>23) hr=0; } void clock::increamentminute() { min++; if(min>59) { min=0; increamenthour(); } } void clock::increamentsecond() { sec++; if(sec>59) { sec=0; increamtentminute(); } } void clock::run(int hour,int minute,int second) { increamenthour(); increamentminute(); increamentsecond(); if(hr==hour&&min==minute&&sec=second) cout<<"鈴響"<<endl; }

⑶ 怎樣用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();
}

⑷ JAVA畫時鍾代碼

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importsun.util.calendar.Gregorian;
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;

publicClockPointer(){
super("Java時鍾");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Imageimage=getToolkit().getImage("clock.gif");
setIconImage(image);
setSize(400,400);
setBackground(Color.white);
//setLocation(300,150);
this.setLocationRelativeTo(null);
setResizable(true);
intdelay=1000;
//創建一個監聽事件

setVisible(true);
ActionListenerdrawClock=newActionListener(){
publicvoidactionPerformed(ActionEventevt){
repaint();
}
};
//創建一個時間計數器,每一秒觸發一次
newTimer(delay,drawClock).start();
}

java.text.SimpleDateFormatfmTime=newjava.text.SimpleDateFormat("HH:mm:ss");
//繪制圖形
publicvoidpaint(Graphicsg){
super.paint(g);
g.setFont(null);
Graphics2Dg2D=(Graphics2D)g;

Insetsinsets=getInsets();
intL=insets.left/2,T=insets.top/2;
h=getSize().height;
g.setColor(Color.white);
//畫圓
g2D.setStroke(newBasicStroke(2.0f));
g.setColor(Color.gray);
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;
//繪制時鍾上的12個字
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.setColor(Color.black);
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=fmTime.format(now.getTime());
//在窗體上顯示時間
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;
g2D.setStroke(newBasicStroke(1.2f));
//擦除秒針
//if(olds_x>0){
//g.setColor(getBackground());
////g.setColor(Color.gray);
//g.drawLine(x0,y0,olds_x,h-olds_y);//(?)
//}
//繪制秒針
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.2f));
//擦除時針
//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){
newClockPointer();
}
}

//整理一下

⑸ java程序 時鍾 整點報時代碼

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class Test extends JFrame implements ActionListener{
Timer t=new Timer(1000,this);
GregorianCalendar calendar=new GregorianCalendar();
JLabel label1=new JLabel(new Date().toString()),
label2=new JLabel("

");

public Test(){
super("TIME");
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(label1);c.add(label2);
t.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200,200,200,100);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Date date=new Date();
calendar.setTime(date);
if(e.getSource()==t){
label1.setText(date.toString());
if(calendar.get(Calendar.MINUTE)==0)//當分鍾為0報時
if(calendar.get(Calendar.SECOND)<=5){//持續5秒報時

if(calendar.get(Calendar.SECOND)%2==0)

label2.setText("現在是北京時間"+calendar.get(Calendar.HOUR_OF_DAY)+"點整。");

if(calendar.get(Calendar.SECOND)%2==1)

label2.setText("
");
}
}
}

public static void main (String[] args) {
new Test();
}
}

⑹ 跪求JAVA時鍾程序~簡單點的~~實訓用~~

import java.awt.*;
import javax.swing.*;
import java.util.*;

//定義測試類
//所有變數名沖突的變數名串1了,不過就效果不好~~
public class TimerTest
{
//定義主函數
public static void main(String[] args)
{

//定義JFrame類的一個對象,並創建該對象
MyTimer1 f = new MyTimer1();
//調用MyTimer的show()方法
f.show();

//----------------------------------------------------

//調用類的構造函數
MyTimer myTimer=new MyTimer();

//調用類的顯示時間函數
myTimer.displayCurrentTime();

//調用類的設置時間函數
myTimer.setCurrentTime();

//調用類的顯示時間函數
myTimer.displayCurrentTime();

//調用類的顯示時間函數
myTimer.displayCurrentTime();
System.exit(0);
}
}

//定義MyTimer類
class MyTimer1 extends JFrame
{ static int count=0; //判斷是否重定義了時間
//構造函數
public MyTimer1()
{
//定義窗口大小
setSize(320, 200);
//定義窗口標題
setTitle("測試自定義時鍾類!");

Container c = getContentPane();
// new ClockCanvas("北京時間", "GMT+8")

c.add(new ClockCanvas("北京時間", "GMT+8"));
}
}

//定義介面
interface TimerListener1
{
void timeElapsed(Timer1 t);
}

class Timer1 extends Thread //類Timer1
{
private TimerListener1 target;
private int interval;

public Timer1(int i, TimerListener1 t)
{
target = t;
interval = i;
setDaemon(true);
}

public void run()
{ try
{
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}

class ClockCanvas extends JPanel //clockcanvas
implements TimerListener1
{
static int seconds = 0;
private String city;

private GregorianCalendar calendar;

//構造函數
public ClockCanvas(String c, String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer1 t = new Timer1(1000, this);
t.start();
setSize(180, 180);
}

//繪制鍾面
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(100, 5, 120, 120);
g.drawOval(101, 6, 118, 118);
//分離時間
double hourAngle = 2 * Math.PI
* (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteAngle = 2 * Math.PI
* (seconds - 15 * 60) / (60 * 60);
double secondAngle = 2 * Math.PI
* (seconds - 15) / 60;

g.drawLine(160, 64, 160 + (int)(20* Math.cos(hourAngle)),
64 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 65, 160 + (int)(20* Math.cos(hourAngle)),
65 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 66, 160 + (int)(20* Math.cos(hourAngle)),
66 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 63, 160 + (int)(20* Math.cos(hourAngle)),
63 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 67, 160 + (int)(20* Math.cos(hourAngle)),
67 + (int)(30 * Math.sin(hourAngle)));

g.drawLine(160, 65, 160 + (int)(32* Math.cos(minuteAngle)),
65 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(160, 64, 160 + (int)(32* Math.cos(minuteAngle)),
64 + (int)(40 * Math.sin(minuteAngle)));

g.drawLine(160, 65, 160 + (int)(55* Math.cos(secondAngle)),
65 + (int)(45 * Math.sin(secondAngle)));

g.drawString(city, 130, 150);//*/
}

public void timeElapsed(Timer1 t)
{
calendar.setTime(new Date());
if(MyTimer1.count==1) {int a=1;

seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond;
seconds+=a;//a為秒自加

repaint();}
else

{ seconds = calendar.get(Calendar.HOUR) * 60 * 60
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
repaint();}
}
}

//定義時鍾類
class MyTimer
implements TimerListener
{
//定義時鍾類的屬性
static int intHour,intMinute,intSecond;

//構造函數
public MyTimer()
{
setCurrentTimeAsSystemTime();
Timer t = new Timer(1000, this); //實例Timer類,裡面有run方法
t.start();
}

//顯示當前時間
public void displayCurrentTime()
{
JOptionPane.showMessageDialog(null,intHour+":"+intMinute+":"+intSecond);
}

//設定當前時間
public void setCurrentTime()
{
//從對話框輸入時,分秒
String strTemp=JOptionPane.showInputDialog(null,"請輸入當前小時(24小時制):");
int iHour=Integer.parseInt(strTemp);

strTemp=JOptionPane.showInputDialog(null,"請輸入當前分:");
int iMinute=Integer.parseInt(strTemp);

strTemp=JOptionPane.showInputDialog(null,"請輸入當前秒:");
int iSecond=Integer.parseInt(strTemp);

//設定當前時間為對話框輸入的時間
if(iHour>=0&&iHour<24)
intHour=iHour;
else intHour=0;
if(iMinute>=0&&iMinute<60)
intMinute=iMinute;
else intMinute=0;
if(iSecond>=0&&iSecond<60)
intSecond=iSecond;
MyTimer1.count++;

// ClockCanvas.seconds=iHour*60*60+iMinute*60+iSecond;

}

//設定當前時間為系統時間,構造函數調用
public void setCurrentTimeAsSystemTime()
{
//定義Date類的一個對象,用來獲取系統時間
Date timeCurrent=new Date();

//將系統的時,分秒設定為當前時間
intHour=timeCurrent.getHours();

intMinute=timeCurrent.getMinutes();

intSecond=timeCurrent.getSeconds();
}

//走時
public void timeElapsed(Timer t)
{
//系統走時

intSecond++;
if (intSecond==60)
{
intMinute++;
intSecond=0;
}

if (intMinute==60)
{
intHour++;
intMinute=0;
}

if (intHour==24)
{
intHour=0;
}

}
}

interface TimerListener //介面了
{
void timeElapsed(Timer t);
}

class Timer extends Thread //類啊!!!
{
private TimerListener target;
private int interval;

public Timer(int i, TimerListener t)
{
target = t;
interval = i;
setDaemon(true); //Thread 裡面方法 目的跟著老大走
}

public void run()
{ try
{
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}

⑺ 用java編寫一個時鍾的程序

我給你做了一下:(第二個功能你自己加吧)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.*;

public class TestTimer extends JFrame implements ActionListener{
Timer timer;
JLabel display = new JLabel();
SimpleDateFormat dateFmt = new SimpleDateFormat("hh:mm:ss");

TestTimer(){

getContentPane().setLayout(new FlowLayout());
timer = new Timer(1000,this);
timer.setRepeats(true);
timer.setInitialDelay(1000);
timer.start();
getContentPane().add(display);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,100);
setVisible(true);

} // TestTimer

public void actionPerformed(ActionEvent e){

if(e.getSource() == timer){

display.setText(dateFmt.format(new Date()));
}

} // actionPerformed

public static void main(String[] args){

TestTimer tt = new TestTimer();
}
} // TestTimer

⑻ 寫一個時鍾的java類


publicclassA{
publicstaticvoidmain(String[]args){
Clockclock=newClock(13,20,38);
System.out.print(clock);
}

}

classClock{
privateinthour;
privateintminute;
privateintsecond;

publicClock(inthour,intminute,intsecond){
super();
this.hour=hour;
this.minute=minute;
this.second=second;
}

publicintgetHour(){
returnhour;
}

publicvoidsetHour(inthour){
this.hour=hour;
}

publicintgetMinute(){
returnminute;
}

publicvoidsetMinute(intminute){
this.minute=minute;
}

publicintgetSecond(){
returnsecond;
}

publicvoidsetSecond(intsecond){
this.second=second;
}

@Override
publicStringtoString(){
return"現在時間是:"+hour+"時"+minute+"分"+second+"秒.";
}

}

⑼ java編寫windows系統的日歷和時鍾

import java.lang.Carlendar;
import java.util.Date;

class Mytime extends Thread{ //繼承 Thread 類,實現多線程

Thread thread = new Thread();
thread.Start();
public void fun()
{
while(true)
{
Date date = new Date();
thread.Sleep(1000); //設置一秒鍾提取一次
txtTime.setText(date) //將時間顯示到文本框里,還要提取一下,你自己搞了
}
}
}

日歷不寫了,
這是動態時間,
你有電腦就要自己調試一下,
我敢肯定是這樣做的,
我現在忘記了一些,
這樣不行你把thread.Start();
放到while裡面去,
但是你改一改絕對會成功,
因為我做了
祝你成功

⑽ 怎麼用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);
}
}

閱讀全文

與java時鍾代碼相關的資料

熱點內容
win10ksm伺服器地址 瀏覽:339
一元二次估演算法 瀏覽:925
一招讀懂源碼 瀏覽:126
為什麼蘋果ipad連接不上伺服器 瀏覽:927
可樂app怎麼用卡密 瀏覽:766
路由器如何打開dhcp伺服器地址 瀏覽:75
谷歌圖片怎麼搜索app 瀏覽:863
江蘇文件夾模具廠 瀏覽:464
安卓系統網線攔截怎麼關閉 瀏覽:31
伺服器和終端是什麼 瀏覽:516
換安卓蘋果照片怎麼弄回去 瀏覽:260
如何向ca中心申請伺服器 瀏覽:187
從java到Android 瀏覽:551
福建加密app哪個好用 瀏覽:382
opengl游戲編程徐明亮pdf 瀏覽:619
單片機row怎麼用 瀏覽:981
虛函數表與反編譯 瀏覽:134
網上的滴滴搶單app是怎麼做的 瀏覽:384
eve手游ios賬號怎麼登錄安卓的 瀏覽:622
linuxoracle12安裝教程 瀏覽:697