onion101325
发贴: 7
积分: 0
|
于 2006-06-17 22:14
//CanvasJs.java
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.text.*; import java.lang.*;
public class CanvasJs extends JFrame{ CanvasJs(String title){ super(title); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); setSize(200,200); setLayout(new FlowLayout()); } public static void main(String[] arg){ CanvasJs f1= new CanvasJs("计时器"); MyCanvas Mc = new MyCanvas(); f1.add(Mc); f1.setVisible(true); } }
class MyCanvas extends Canvas implements Runnable{ boolean flag = false; long startT,endT; Thread t1 = new Thread(this); MyCanvas(){ addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ startT=endT=System.currentTimeMillis(); repaint(); flag = true; t1.start(); //可能错在这里 } public void mouseReleased(MouseEvent e){ flag = false; endT=System.currentTimeMillis(); repaint(); } }); setBackground(Color.RED); setSize(100,50); setFont(new Font(null,Font.BOLD|Font.ITALIC ,22)); setVisible(true); } public void paint(Graphics g){ Date Dt= null; SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss"); try{ Dt = sdf.parse("00:00:00"); }catch(Exception ee){ } Dt.setTime(Dt.getTime()+endT-startT); g.drawString(sdf.format(Dt),6,33); }
public void run(){ while(flag){ try{ Thread.sleep(500); }catch(Exception e){ } endT=System.currentTimeMillis(); repaint(); } } }
这个程序有我错误,小的初学,弄不明白,请高手讲讲! 谢谢!
|