import java.awt.*;

站在用戶的角度思考問題,與客戶深入溝通,找到桓仁網(wǎng)站設(shè)計(jì)與桓仁網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋桓仁地區(qū)。
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClockApplet extends Applet implements Runnable //Applet支持線程
{
private Thread athread; //線程
private SimpleDateFormat sdateformat; //日期格式
public void init()
{
this.setBackground(Color.white);//背景顏色設(shè)為白色
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh時(shí)mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);
Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);
//半徑
int R_H = 20,R_M = 4,R_S = 4;
//時(shí)針的坐標(biāo)
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour = 3 hour = 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour = 6 hour = 12 )
{
H_y = R_H*Math.abs(hour - 12);
}
else
{
H_y = R_H*hour;
}
//分針的坐標(biāo)
int M_x;
int M_y;
if(minute == 0)
{
minute = 60;
}
if( minute = 15 minute = 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute = 30 minute 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}
//秒針的坐標(biāo)
int S_x;
int S_y;
if(second == 0)
{
second = 60;
}
if( second = 15 second = 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second = 30 second = 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}
// g.drawString(String.valueOf(second),25,50);
// g.drawString(String.valueOf(minute),25,60);
// g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90);
g.drawOval(0,0,120,120);//距離相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);
g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);
g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);
g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一個(gè)點(diǎn)表示起點(diǎn),另一個(gè)表示終點(diǎn)
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);
}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
利用死循環(huán)和線程,讓線程在循環(huán)中每sleep1秒,重新獲取下系統(tǒng)時(shí)間不就是動(dòng)態(tài)顯示時(shí)間了嗎
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
//每一秒刷新下時(shí)間
try {
Thread.sleep(1000);//sleep是以ms為單位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你可以試下代碼,看看是不是你要的效果
可以的,可以實(shí)時(shí)動(dòng)態(tài)顯示當(dāng)前時(shí)間與當(dāng)前日期,代碼結(jié)構(gòu)簡(jiǎn)潔、清晰、明了,(網(wǎng)絡(luò)搜集,供參考)
知識(shí)的匯總:
1.HTML5
2.CSS3
3.JavaScript
重難點(diǎn)匯總:
1.各個(gè)指針的旋轉(zhuǎn)角度的獲取,首先要明確以下概念:
一周為360度、12小時(shí)、60分鐘、60秒;
公式:一周的度數(shù)/一周的時(shí)間;
即得出時(shí)針每過一小時(shí)要旋轉(zhuǎn)30度;
分針每過一分鐘要旋轉(zhuǎn)6度;
秒針每過一秒鐘要旋轉(zhuǎn)6度;
下面是代碼部分:
HTML:
div id="box"
div id="h"/div
div id="min"/div
div id="s"div class="cen"/div/div
div id="data"/div
/div
CSS3:
body{
background-color: #aaa;
margin: 0px;
padding: 0px;
}
#box{
width: 400px;
height: 400px;
border-radius: 100%;
background: url(img/4706.jpg_wh860.jpg)0px 0px no-repeat;
background-size: 400px;
position: absolute;
left: 500px;
top: 200px;
}
#h{
width: 100px;
height: 10px;
background-color: red;
position: relative;
top: 195px;
left: 200px;
}
#min{
width: 140px;
height: 10px;
background-color: yellow;
position: relative;
top: 185px;
left: 200px;
}
#s{
width: 180px;
height: 10px;
background-color: blue;
position: relative;
top: 175px;
left: 200px;
}
.cen{
width: 10px;
height: 10px;
background-color: white;
border-radius: 100%;
}
#data{
position: relative;
top: 100px;
left: 150px;
color: red;
font-size: 20px;
}
JavaScript:
function tim(){
var d = new Date(),//獲取當(dāng)前系統(tǒng)時(shí)間
year = d.getFullYear(),//得到當(dāng)前年份
mon = d.getMonth(),//得到當(dāng)前月份
date = d.getDate(), //得到當(dāng)前日期
hours = d.getHours(), //得到當(dāng)前小時(shí)
minutes = d.getMinutes(), //得到當(dāng)前分鐘
seconds = d.getSeconds();//得到當(dāng)前秒
var hou = "";
if(hours12){
hou = "下午";
}
else{
hou = "上午";
}
document.getElementById("data").innerHTML= year+"年"+mon+"月"+date+"日"+"
"+hou;
var n = document.getElementById("s");//獲取秒針I(yè)D
n.style.transform = "rotate("+(seconds*6-90)+"deg)";//通過當(dāng)前秒數(shù),得到秒針旋轉(zhuǎn)度數(shù)
n.style.transformOrigin = "left";//設(shè)置秒針旋轉(zhuǎn)的基點(diǎn)
var i = document.getElementById("min");//獲取分針I(yè)D
i.style.transform = "rotate("+(minutes*6-90)+"deg)"http://通過當(dāng)前分鐘數(shù),得到分針旋轉(zhuǎn)度數(shù)
i.style.transformOrigin = "left";//設(shè)置分針旋轉(zhuǎn)的基點(diǎn)
var h = document.getElementById("h");//獲取時(shí)針I(yè)D
h.style.transform = "rotate("+((hours*30)+(minutes*0.5)-90)+"deg)"http://通過當(dāng)前小時(shí)數(shù),得到時(shí)針旋轉(zhuǎn)度數(shù)
h.style.transformOrigin = "left";//設(shè)置時(shí)針旋轉(zhuǎn)的基點(diǎn)
}
setInterval("tim()",1000)
本文名稱:java動(dòng)態(tài)時(shí)間代碼 java編寫一個(gè)動(dòng)態(tài)日歷
本文URL:http://chinadenli.net/article12/ddgcpdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、App設(shè)計(jì)、自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)