日期:2014-05-20  浏览次数:20900 次

java 图形界面问题!实在找不到哪里错···
[code=Java][/code]
文件1:
package com.stopwatch;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

 
public class StopWatch extends Canvas implements Runnable {
 private long startTime=0;
 private long endTime=0;
 private boolean bStart =false; 
 
 public StopWatch(){
  enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  setSize(60,70);
 }
 protected void processMouseEvent(MouseEvent e){
  if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  bStart = true;
  new Thread(this).start();
  startTime = endTime =System.currentTimeMillis();
  repaint();
  }else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
  endTime = System.currentTimeMillis();
  repaint();
  bStart = false;
  }
 }
 
 public void paint(Graphics g){
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  Date elapseTime =null;
  
  try {
  elapseTime =sdf.parse("00:00:00");
  } catch (ParseException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  elapseTime.setTime(endTime-startTime+elapseTime.getTime());
  g.draw3DRect(50, 50, 30, 10, true);
  String display = sdf.format(elapseTime);
  g.drawString(display, 25, 8);
  
 }
 
 public void run(){
  while (bStart) {
  try {
  Thread.sleep(300);
  } catch (Exception e) {
  // TODO: handle exception
  e.printStackTrace();
  }
  endTime = System.currentTimeMillis();
  repaint();
  }
 }

 
}
文件2:
package com.stopwatch;
import java.awt.Frame;
public class TestStopWatch extends Frame{
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  StopWatch sw = new StopWatch();
  Frame f = new Frame();
  f.add(new StopWatch());
  f.setSize(100, 100);
  f.setVisible(true);
 }

 
}
我找不到哪里有错····帮忙找一下!

------解决方案--------------------
StopWatch 没看明白
Java code
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class TestStopWatch {
  /**
   * @param args
   */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final StopWatch sw = new StopWatch();
        final Frame f = new Frame();
        f.add(sw);
        f.addWindowListener(new WindowAdapter(){
            @Override public void windowClosing(WindowEvent e){
                f.dispose();
            }
        });
        f.setSize(100, 100);
        f.setVisible(true);
    }
}

------解决方案--------------------


你的这两段代码放在一起就可以实现了:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.awt.Frame;
 
public class StopWatch extends Canvas implements Runnable {
 private long startTime=0;
 private long endTime=0;
 private boolean bStart =false; 
 
 public StopWatch(){
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setSize(60,70);
 }
 protected void processMouseEvent(MouseEvent e){