Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求助!谢谢各位不吝赐教.....
onion101325





发贴: 7
积分: 0
于 2006-06-17 22:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
//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();
    }
  }
}

这个程序有我错误,小的初学,弄不明白,请高手讲讲!
谢谢!



作者 Re:求助!谢谢各位不吝赐教..... [Re:onion101325]
wangjichen00





发贴: 3
积分: 0
于 2006-06-18 21:02 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
class MyCanvas extends Canvas implements Runnable{
boolean flag = false;
long startT,endT;
Thread t1 = null;
MyCanvas(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
startT=endT=System.currentTimeMillis();
repaint();
flag = true;
if (t1 == null){
t1 = new Thread((Runnable)e.getSource());
t1.start();
}
}
public void mouseReleased(MouseEvent e){
flag = false;
endT=System.currentTimeMillis();
repaint();
t1.interrupt();
t1 = null;
}
});
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();
}
}



作者 Re:求助!谢谢各位不吝赐教..... [Re:onion101325]
onion101325





发贴: 7
积分: 0
于 2006-06-22 18:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢了!!
用了你这条语句 “ new Thread((Runnable)e.getSource()); ”

我用这条语句实现了: “ new Thread(MyCanvas.this).start(); ”




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923