超级简单的java时钟,仅30多行代码
import java.awt.*;import java.util.*;
import javax.swing.*;
public class HyClock extends JFrame implements Runnable {
public static JLabel tm;
public void run() {
while (true) {
Calendar c = Calendar.getInstance();
String nowtime = c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
tm.setText(nowtime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public static void main(String[] agrs) {
HyClockhytime = new HyClock();
hytime.setLayout(new FlowLayout());
tm = new JLabel();
tm.setFont(new Font("宋体", 1, 40));
tm.setForeground(Color.RED);
hytime.add(tm);
hytime.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
hytime.setSize(300, 100);
hytime.setVisible(true);
hytime.setLocation(400, 400);
Thread t = new Thread(hytime);
t.start();
}
} 有意思吗{:1_911:} 补充一个用JavaScript写的时钟,是个网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时间</title>
<script type="text/javascript">
function disTime(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
if (month<10) {
month = "0"+month;
}
if (day<10) {
day = "0"+day;
}
if (hour<10) {
hour = "0"+hour;
}
if (minute<10) {
minute = "0"+minute;
}
if (second<10) {
second = "0"+second;
}
document.getElementById("clock").innerHTML="现在是:"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;
}
onload = disTime;
var time = setInterval("disTime()",1000);
</script>
</head>
<body>
<div id="clock">
</div>
</body>
</html>
效果图http://139.129.23.15/java.png 感谢分享了,。 喜欢!感谢分享 代码在网页运行不了吧 吾爱文 发表于 2018-8-9 19:01
代码在网页运行不了吧
网页运行看我的评论 吧边框去掉吧 感谢分享。