扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
package cn.test;
成都创新互联公司凭借在网站建设、网站推广领域领先的技术能力和多年的行业经验,为客户提供超值的营销型网站建设服务,我们始终认为:好的营销型网站就是好的业务员。我们已成功为企业单位、个人等客户提供了网站建设、网站设计服务,以良好的商业信誉,完善的服务及深厚的技术力量处于同行领先地位。
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Time extends JFrame {
private JTextField TimeBox = new JTextField();
private Timer timer = new Timer() ;
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public Time() {
TimeBox.setBounds(0, 0, 100, 50);
add(TimeBox);
//方法1 定时器方法
// TimerTask task = new TimerTask() {
// @Override
// public void run() {
// TimeBox.setText(sdf.format(new Date()));
// }
// };
// timer.schedule(task, 1000,1000) ;
//方法2 线程休眠方法
Display timedisplay = new Display();
timedisplay.start() ;
}
private class Display extends Thread {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public Display() {
System.out.println("display");
}
@Override
public void run(){
while(true){
System.out.println( "Thread is running..." );
TimeBox.setText(sdf.format(new Date()));
try {
Thread.sleep(1000) ;
} catch (InterruptedException e) {
System.out.println("sleep error!!");
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Time t = new Time() ;
t.setSize(300, 200) ;
t.setResizable(false) ;
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true) ;
}
}
private Shape rect; //背景矩形
private Font font; //设置字体
private Date date; //现在的时间
private Thread time; //时间线程
private CanvasPanel canvas;
public static void main(String[] args) {
new TimerTest20140930();
}
public TimerTest20140930(){
super("绘制文本");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
rect = new Rectangle2D.Double(10,10,200,100);
font = new Font("宋体",Font.BOLD,16);
canvas=new CanvasPanel();
add(canvas);
time = new Thread(new Runnable(){
public void run(){
while(true){
canvas.repaint();
try{
Thread.sleep(1000);
}catch(Exception ex){
}
}
}
});
time.start();
setLocationRelativeTo(null);
setVisible(true);
}
class CanvasPanel extends Canvas {
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fill(rect);
g2.setColor(Color.BLUE);
g2.setFont(font);
g2.drawString("现在的时间是", 20, 30);
date = new Date();
g2.drawString(String.format("%tr", date), 50, 60);
}
}
html
head
title时钟特效/title
script type="text/javascript"
function disptime(){
var today = new Date(); //获得当前时间
var hh = today.getHours(); //获得小时、分钟、秒
var mm = today.getMinutes();
var ss = today.getSeconds();
/*设置div的内容为当前时间*/
document.getElementById("myclock").innerHTML="h1现在是:"+hh+":"+mm+":"+ss+"h1";
document.getElementById("myClock1").value=hh+":"+mm+":"+ss;
/*
使用setTimeout在函数disptime()体内再次调用setTimeout
设置定时器每隔1秒(1000毫秒),调用函数disptime()执行,刷新时钟显示
*/
var myTime=setTimeout("disptime()",1000);
}
/script
/head
body onload="disptime()"
div id="myclock"/div
input type="text" id="myClock1" value=""/input
/body
/html
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流