while(true){
Date date=new Date(SystemcurrentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Systemoutprintln(date);
//每一秒刷新下时间
try {
Threadsleep(1000);//sleep是以ms为单位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
你可以试下代码,看看是不是你要的效果
1、如果计算器没有显示时间界面,按下“时间”按钮,手动调到时间界面。
2、然后,按下“设定”按钮,小时的数字开始闪烁。
3、按下“上调”或者“下调”按钮,调整到所需要的小时。
4、再次按下“设定”按钮,分的数字开始闪烁,按下“上调”或者“下调”按钮,调整到所需要的分钟。
5、最后,按下“设定”按钮,秒的数字开始闪烁,按下“上调”或者“下调”按钮,调整到所需要的秒数。
6、可以调整正常时间了,具体完成图如下。
显示实时时间的JPanel1重写JPanel的paint方法,在JPanel上画上实时时间的字符串。(下边的例子是用的这个方法)
2在JPanel加个label,在上面设置实时时间的字符串。
--------------------------------------------------------------------------
import javaawtGraphics;
import javatextSimpleDateFormat;
import javautilDate;
import javautilTimer;
import javautilTimerTask;
import javaxswingJFrame;
import javaxswingJPanel;
public class ShowTimeApp extends JFrame {
// 时间格式
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public ShowTimeApp() {
// 设置显示窗口的参数
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 居中显示
setLocationRelativeTo(null);
// 窗口大小
setSize(200, 150);
// 窗口不可改变大小
setResizable(false);
// 添加时间的JPanel
getContentPane()add(new TimePanel());
// 显示
setVisible(true);
// 设置时间变化的任务
Timer timer = new Timer();
timerschedule(new ShowTime(), new Date(), 1000);
}
class ShowTime extends TimerTask {
public void run() {
// 刷新
repaint();
}
}
class TimePanel extends JPanel {
public void paint(Graphics g) {
superpaint(g);
// 显示时间
gdrawString(sdfformat(new Date()), 10, 10);
}
}
public static void main(String[] args) {
new ShowTimeApp();
}
}这是一段javascript写的,无论是java或javascript原理都一样,进攻参考
<!-- Begin
function run() {
with (documentagecalc) {
dd = parseInt(dayselectedIndex) + 1;
mm = parseInt(monthselectedIndex) + 1;
yy = yearvalue;
if (yylength != 4 || isNaN(yy)) {
documentagecalctimealivevalue = "请输入4位数的年份";
documentagecalcyearselect();
documentagecalcyearfocus();
return;
}
}
days = new Date();
gdate = daysgetDate();
gmonth = daysgetMonth();
gyear = daysgetYear();
if (gyear < 2000) gyear += 1900;
age = gyear - yy;
if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
age = age;
} else {
if (mm <= (gmonth)) {
age = age;
} else {
age = age - 1;
}
}
if (age == 0)
age = age;
documentagecalctimealivevalue = "你已经满了 " + age+ " 周岁 \n\n";
if (mm <= (gmonth + 1))
age = age - 1;
if ((mm == (gmonth + 1)) && (dd > parseInt(gdate)))
age = age + 1;
var m;
var n;
if (mm == 12) n = 31 - dd;
if (mm == 11) n = 61 - dd;
if (mm == 10) n = 92 - dd;
if (mm == 9) n = 122 - dd;
if (mm == 8) n = 153 - dd;
if (mm == 7) n = 184 - dd;
if (mm == 6) n = 214 - dd;
if (mm == 5) n = 245 - dd;
if (mm == 4) n = 275 - dd;
if (mm == 3) n = 306 - dd;
if (mm == 2) { n = 334 - dd; if (leapyear(yy)) n++; }
if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n++; }
if (gmonth == 1) m = 31;
if (gmonth == 2) {
m = 59;
if (leapyear(gyear)) m++;
}
if (gmonth == 3) { m = 90; if (leapyear(gyear)) m++; }
if (gmonth == 4) { m = 120; if (leapyear(gyear)) m++; }
if (gmonth == 5) { m = 151; if (leapyear(gyear)) m++; }
if (gmonth == 6) { m = 181; if (leapyear(gyear)) m++; }
if (gmonth == 7) { m = 212; if (leapyear(gyear)) m++; }
if (gmonth == 8) { m = 243; if (leapyear(gyear)) m++; }
if (gmonth == 9) { m = 273; if (leapyear(gyear)) m++; }
if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }
if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }
if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++; }
months = age 12;
months += 12 - parseInt(mm);
months += gmonth;
totdays = (parseInt(age) 365);
totdays += age / 4;
totdays = parseInt(totdays) + gdate + m + n;
if (gmonth == 1) p = 31 + gdate;
if (gmonth == 2) {
p = 59 + gdate;
if (leapyear(gyear)) m = m+1;
}
if (gmonth == 3) { p = 90 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 4) { p = 120 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 5) { p = 151 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 6) { p = 181 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 7) { p = 212 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 8) { p = 243 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 9) { p = 273 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p++; }
weeks = (age 365) + n + p;
weeks = weeks / 7;
etcdays = parseFloat(weeks) - parseInt(weeks);
etcdays = Mathround(etcdays 7);
weeks = parseInt(weeks);
etcdays += parseInt(age / 4);
if (etcdays > 7)
weeks += parseInt(etcdays / 7);
documentagecalctimealivevalue += " 或 " + months + " 月龄\n";
documentagecalctimealivevalue += " 或 " + weeks + " 周龄\n";
documentagecalctimealivevalue += " 或 " + totdays + " 日龄\n";
var time = new Date();
ghour = timegetHours();
gmin = timegetMinutes();
gsec = timegetSeconds();
hour = ((age 365) + n + p) 24;
hour += (parseInt(age / 4) 24);
documentagecalctimealivevalue += " 或 " + hour + " 小时\n";
var min = (hour 60) + gmin;
documentagecalctimealivevalue += " 或 " + min + " 分钟\n";
sec = (min 60) + gsec;
documentagecalctimealivevalue += " 或 " + sec + " 秒钟";
mm = mm - 1;
var r;
if (mm == 0) r = 0;
if (mm == 1) r = 31;
if (mm == 2) { r = 59; if (leapyear(gyear)) m++; }
if (mm == 3) { r = 90; if (leapyear(gyear)) r++; }
if (mm == 4) { r = 120; if (leapyear(gyear)) r++; }
if (mm == 5) { r = 151; if (leapyear(gyear)) r++; }
if (mm == 6) { r = 181; if (leapyear(gyear)) r++; }
if (mm == 7) { r = 212; if (leapyear(gyear)) r++; }
if (mm == 8) { r = 243; if (leapyear(gyear)) r++; }
if (mm == 9) { r = 273; if (leapyear(gyear)) r++; }
if (mm == 10) { r = 304; if (leapyear(gyear)) r++; }
if (mm == 11) { r = 334; if (leapyear(gyear)) r++; }
mm = mm + 1;
r = parseInt(r) + parseInt(dd);
if ((mm >= (gmonth + 1)) && (dd > gdate)) {
bday = r - m - gdate;
}
else {
if ((leapyear(gyear)) && ((mm > 2) && (dd < 29))) {
a = 366;
} else {
a = 365;
}
bday = a + (r - m - gdate);
}
nhour = 24 - parseInt(ghour);
nmin = 60 - parseInt(gmin);
nsec = 60 - parseInt(gsec);
while (bday > 366) bday -= 365;
if (((bday == 366) && (leapyear(gyear)) || ((bday == 365) && (!leapyear(gyear))))) {
documentagecalctimealivevalue += "\n\nSenlon祝贺您!今天是你的生日!祝你生日快乐!";
} else {
documentagecalctimealivevalue += "\n\n另外,你下一个生日距今还有:\n"
+ bday + " 天 " + nhour + " 小时 " + nmin + " 分 " + nsec + " 秒";
setTimeout("run()", 1000);
}
}
function leapyear(a) {
if (((a%4 == 0) && (a%100 != 0)) || (a%400 == 0))
return true;
else
return false;
}
// End -->
来自 >好像不能用documentwrite
换<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 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=myTimegetYear();
date=myTimegetDate();
hours=myTimegetHours();
minutes=myTimegetMinutes();
seconds=myTimegetSeconds();
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[myTimegetMonth()]+date+"日"+" "+dayArray[myTimegetDay()]+
"</font><br><font size=2>当前时间:</font>"+
"<br><font size=4 face=Arial>"+hours+":"+minutes+":"+seconds+" "+suf+"</font>";
DTinnerHTML=theTime;
setTimeout("showTime()",1000);
}
-->
</script>
<body onload="showTime()">
<span id=DT style="position:absolute;left=35px;top=15px"></span>
</body>
</html>java编写时钟程序_Java实现时钟小程序 原创
2021-02-17 00:47:37
无可就是九头鸟
码龄6年
关注
哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋。然后五月中旬的时候写了一个2048小游戏,由于写完第二个版本第二天就生病了,休养了好几天才缓过来,最后嫌麻烦就没发园子里了,直接挂Github上了。然后又忙着准备期末,期末玩休息了5天又开始数据库课程设计,用Java写了一个小系统,花了一个星期左右,写了差不多3500行了。然后写完就放学了,放学的日子就感觉自己开始懒散了,静不下心来写东西,每天睡觉,看一些无聊的视频。。。前几天算是缓过神来,找回了脑子,于是又开始想着在放弃Java之前再多写一点东西,于是花了两个下午写了一个时钟,感觉看着还行。
-----------------------------------以下是正经话-----------------------------------
其实这个程序并不难,主要是用Java 2D画图,弄一个线程,每隔一秒,依照时针分针秒针各自的速度重新计算它们走过的角度(相对12点方向顺时针偏离的角度),利用这个角度算出它们各自端点的坐标,重绘一次表针,达到刷新的目的,最后时限走动的效果,思路很简单,清晰明了。但是关键是这个画图呀,想着很容易,重写继承过来的JFrame里的paint函数就行,最后发现效果并不是期待的那样,而是一闪一闪的,于是去寻找原因,结果,尼玛,原来水这么深,一下子触到了知识盲区,什么repaint的实现细节,先调用update,再调用paint,update函数又是如何工作的,慢慢弄懂了它为什么会一闪一闪的原因,然后寻找解决办法,然后就看到了什么双缓冲原理,WTF!什么玩意呀,根本没听过,然后乖乖地去看那东西的实现原理,又看到有人建议不要去重写paint方法,说是会遇到诸多意想不到的问题,建议去重写paintComponent方法,可是书本上画图的例子都是重写paint方法呀,况且我也没听过paintComponent,无奈,又去看了一下paint的工作原理,原来它会调用paintComponent,最后放弃了自己实现双缓冲,乖乖改用了paintComponent。。。
其实我还遇到了几个脑残的问题,比如在画秒针时,直接把画时针的部分代码复制下来,最后忘了改掉走动的速度,结果运行时还在想秒针怎么走不动呢?原谅我是个逗逼,还有什么秒针逆时针走动,最后才发现算坐标时误把y轴正方向当成了向上,哎。。。
Anyway,最后运行的效果还是挺好的
3c55d177347f0c89c364f17fe0859265png
-----------------------------------以下是代码-----------------------------------
1 import javaawt;2 importjavautilDate;3 import javaxswing;4
5 public class Clock extendsJComponent{6 /
7 8 /
9 private static final long serialVersionUID = -5379472973578609775L;10 private Font f = new Font("微软雅黑",FontPLAIN,15);11 private Font f2 = new Font("微软雅黑",FontBOLD,15);12 private JLabel l = new JLabel("当前时间:");13 private JLabel display = newJLabel();14 private JLabel display2 = new JLabel("");15 private int hour = 0;16 private int min = 0;17 private int sec = 0;18 private Date now = newDate();19 privateGraphics2D g;20 final double PI =MathPI;21 private String strTime = "";22
23 @SuppressWarnings("deprecation")24 publicClock(){25 add(l);26 lsetBounds(120, 320, 80, 20);27 lsetFont(f);28 add(display);29 displaysetBounds(195, 320, 80, 20);30 displaysetFont(f);31 displaysetBorder(BorderFactorycreateLineBorder(Colorblack));32 add(display2);33 display2setBounds(90, 350, 250, 20);34 display2setFont(f);35 hour =nowgetHours();36 min =nowgetMinutes();37 sec =nowgetSeconds();38 setVisible(true);39 }40
41 public voidpaintComponent(Graphics g1){42 doublex,y;43 superpaintComponent(g1);44 g =(Graphics2D) g1;45 //反锯齿开关开
46 gsetRenderingHint(RenderingHintsKEY_ANTIALIASING, RenderingHintsVALUE_ANTIALIAS_ON);47
48 //画表盘
49 gsetPaint(new GradientPaint(5,40,Colorblue,15,50,Coloryellow,true));50 gsetStroke( new BasicStroke(3,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL));51 gdrawOval(75, 40, 250, 250);52 gfillOval(195, 160, 10, 10);53 gsetColor(Colorblack);54
55 //画60个点
56 for(int i = 0;i < 60;i++)57 {58 double[] co = new double[2];59 co = paint_Dot(i 2 PI / 60);60 x = co[0];61 y = co[1];62 if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
63 {64 gfillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);65 }66 else//其他小点
67 {68 gfillOval((int)(x - 25 + 200),(int)(y - 25 + 165),5,5);69 }70 }71
72 //画四个数字
73 gsetFont(f2);74 gdrawString("3", 300, 171);75 gdrawString("6", 195, 273);76 gdrawString("9", 91, 171);77 gdrawString("12", 190, 68);78
79 //画时针,分针,秒针
80 paint_HourPointer(hour3600 + min60 + sec,g);//时针走过的秒数
81 paint_MinutePointer(min60 + sec,g);//分针走过的秒数
82 paint_SecondPointer(sec,g);//秒针走过的秒数
83 }84
85 public voidshowUI(){86 newThread() {87 @SuppressWarnings("deprecation")88 public voidrun() {89 while (true)90 {91 now = newDate();92 hour =nowgetHours();93 min =nowgetMinutes();94 sec =nowgetSeconds();95 try{96 Threadsleep(1000);97 } catch(InterruptedException ex) {98 exprintStackTrace();99 }100 showTime();101 repaint();102 }103 }104 }start();105 }106
107 public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
108 doublex,y,angle;109 angle = second PI / 21600;//时针的速度为PI/21600 (rad/s)
110 x = 200 + 60 Mathsin(angle);111 y = 165 - 60 Mathcos(angle);112 gsetStroke( new BasicStroke(5,BasicStrokeCAP_BUTT,BasicStrokeJOIN_ROUND));113 gsetPaint(new GradientPaint(200,165,Colorred,260,165,Colorblue,true));114 gdrawLine(200, 165, (int)x, (int)y);115 }116
117 public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
118 doublex,y,angle;119 angle = second PI / 1800;//分针的速度为PI/1800 (rad/s)
120 x = 200 + 80 Mathsin(angle);121 y = 165 - 80 Mathcos(angle);122 gsetStroke( new BasicStroke(3,BasicStrokeCAP_BUTT,BasicStrokeJOIN_ROUND));123 gsetPaint(new GradientPaint(200,165,Colormagenta,280,165,Colorblue,true));124 gdrawLine(200, 165, (int)x, (int)y);125 }126
127 public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
128 doublex,y,x1,y1,x2,y2,x3,y3,angle;129 double cos = 90 / Mathsqrt(8125);//9090+55
130 double sin = 5 / Mathsqrt(8125);131 angle = second PI / 30;//时针的速度为PI/30 (rad/s)
132 x = 200 + 95 Mathsin(angle);133 y = 165 - 95 Mathcos(angle);134 x1 = 200 + 20 Mathsin(angle +PI);135 y1 = 165 - 20 Mathcos(angle +PI);136 x2 = 200 + Mathsqrt(8125) ( Mathsin(angle)cos - Mathcos(angle)sin ); //sin(a-b)
137 y2 = 165 - Mathsqrt(8125) ( Mathcos(angle)cos + Mathsin(angle)sin ); //cos(a-b)
138 x3 = 200 + Mathsqrt(8125) ( Mathsin(angle)cos + Mathcos(angle)sin ); //sin(a+b)
139 y3 = 165 - Mathsqrt(8125) ( Mathcos(angle)cos - Mathsin(angle)sin ); //cos(a+b)
140 gsetStroke( new BasicStroke(2,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL));141 gsetPaint(new GradientPaint(180,165,ColorCYAN,295,165,ColorMAGENTA,true));142 gdrawLine((int)x1, (int)y1, (int)x, (int)y);143 gdrawLine((int)x2, (int)y2, (int)x, (int)y);144 gdrawLine((int)x3, (int)y3, (int)x, (int)y);145 }146
147 public double[] paint_Dot(doubleangle){148 double[] co = new double[2];149 co[0] = 115 Mathcos(angle);//横坐标
150 co[1] = 115 Mathsin(angle);//纵坐标
151 returnco;152 }153
154 @SuppressWarnings("deprecation")155 private voidshowTime(){156 String date;157 int hour_temp = hour,min_temp = min,sec_temp =sec;158 sec_temp += 1;159 if(sec_temp >= 60)160 {161 sec_temp = 0;162 min_temp += 1;163 }164 if(min_temp>=60){165 min_temp=0;166 hour_temp+=1;167 }168 if(hour_temp < 10)169 strTime = "0" + hour_temp + ":";170 else
171 strTime = "" + hour_temp + ":";172
173 if(min_temp < 10)174 strTime = strTime + "0" + min_temp + ":";175 else
176 strTime = strTime + "" + min_temp + ":";177
178 if(sec < 10)179 strTime = strTime + "0" +sec_temp;180 else
181 strTime = strTime + "" +sec_temp;182 //在窗体上设置显示时间
183 date = " " + (nowgetYear()+1900) + "年" + (nowgetMonth()+1) + "月" + nowgetDate() + "日 " + "星期";184 switch(nowgetDay()) {185 case 1:186 date += "一";187 break;188 case 2:189 date += "二";190 break;191 case 3:192 date += "三";193 break;194 case 4:195 date += "四";196 break;197 case 5:198 date += "五";199 break;200 case 6:201 date += "六";202 break;203 case 7:204 date += "日";205 break;206 }207 date += " CST";208 strTime = " " +strTime;209 displaysetText(strTime);210 display2setText(date);211 }212
213 public static voidmain(String args[]){214 Clock c = newClock();215 cshowUI();216 JFrame f = new JFrame("Clock By XJX");217 Image img=ToolkitgetDefaultToolkit()getImage("titlegif");//窗口图标
218 fsetIconImage(img);219 fsetSize(400,400);220 fsetResizable(false);221 fadd(c, BorderLayoutCENTER);222 fsetLocationRelativeTo(null);223 fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);224 fsetVisible(true);225 }226 }
相关资源:仿真和代码)_51单片机做实时时钟显示-小程序文档类资源-CSDN文库
文章知识点与官方知识档案匹配
Java技能树首页概览
91513 人正在系统学习中
打开CSDN APP,看更多技术内容
不会编程也能亲手编写一个桌面数字小时钟程序,仅3步轻松实现_程序员笑
第一步 打开电脑自带的记事本并编写以下脚本。 源码: @echo off Title 小时钟丨编程狮(w3cschoolcn) @mode con cols=50lines=5color00:main cls echoecho 时间:%time%echoecho 日期:%date%echoping-n20000>nulgotomain
继续访问
OpenGL织梦之旅第二章编写一个的钟表程序_plusplus7的博客-CSDN
在配置好了glut环境以后,运行了第一个opengl程序以后,我们现在就来尝试编写一个实用一点的程序——时钟程序。 如图: 制作这样一个程序,对于初学者来说,感觉上或许很难,但是只要我们一步一步地慢慢来,总会有熟练掌握的那一天。“包子要
继续访问
Java时钟程序(绘制了一个带时针 分针 秒针的仿真时钟)
这是我自己写的一个java时钟程序 当然你可以自己改动 喜欢就拿去吧
Java时钟小程序
Java时钟小程序,可以显示出当前的系统时间,
java 钟_JAVA时钟 - 代码块 - 扣丁书屋 - >import javautil;
import javatextSimpleDateFormat;
public class test
{
public static void main (String args[])
{
Date d = new Date();
long longtime = dgetTime();
Systemoutprintln(longtime);
//你获得的是上面的long型数据吧
String time = dtoLocaleString();
//你可以简单的得到本地化时间,本来就是String类型的就不用转换了
Systemoutprintln(time);
//也可以自己用SimpleDateFormat这个函数把它变成自己想要的格式,注意需要import javatextSimpleDateFormat;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Systemoutprintln(sdfformat(longtime));
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)