楼主:怎么可以把你的类命名为Timer呢?在javax.swing.*里面也有一个Timer类。还真不知你如何编译通过的呢。换个类名先。

解决方案 »

  1.   

    TO:楼上tomcatjava(小鱼儿) 这属于不同包中的同名类,为什么不可以编译通过呢?
      

  2.   

    我又不是放在java包下面的,怎么命名都可以。
      

  3.   

    上面的label我一开始用到,后来删掉了,不放弃其他方面。 请后面的朋友不用指出这点
      

  4.   

    import java.awt.*;
    import java.applet.*;
    public class Stopwatch extends Applet {        Repainter repainter = new Repainter( this ); // the Repainter class regularly repaints
            Thread tRepainter = new Thread( repainter, "Repainter thread" );        // define the buttons of the stopwatch
            Button start = new Button( "Start" );
            Button stop  = new Button( "Stop" );
            Button reset = new Button( "Reset" );        Label counter = new Label( "0" );  // the Label "counter" is the time display        long msStart = 0; // time the stopwatch started running
            long msTotal = 0; // accumulated run time so far        // the init() method is the 'main' for applets
    public void init() {
            // layout the components from the left of the frame
            setLayout(new FlowLayout( FlowLayout.LEFT ));
            resize( 300, 60 );        // start the repaint thread
            tRepainter.start();
            // but wait a bit before actually repainting
            tRepainter.suspend();        // add our controls to the frame
            add( start );
            add( stop );
            add( reset );        add( counter );        msStart = 0;
            msTotal = 0;        // initialize the display to 0.0
            _updateCounter();}        // MS Windows-ish handler of all events for the applet
    public boolean handleEvent(Event e) {        // e.id is the event type
            switch (e.id) {
                    // I think I should actually handle DESTROY differently for applet vs application
            case Event.WINDOW_DESTROY:
                    repainter.exit();   // should stop the thread by returning from the 'run' method
                    // (after the sleep?  who cares...)
                    System.exit(0);
                    return true;                // ACTION_EVENTS are the button pushes, etc.
            case Event.ACTION_EVENT:
                    // I could rework this section to delegate functionality to
                    // a "StopwatchState" object (Pattern: State)
                    if( e.target == start )
                    {
                            if( 0 == msStart ) // not started
                            {
                                    // start ticking
                                     msStart = System.currentTimeMillis();
                                    _updateCounter();
                                    tRepainter.resume();
                            }
                    }
                    else if( e.target == stop )
                    {
                            if( 0 != msStart ) // running
                            {
                                    // stop ticking, and add up the total
                                    msTotal += System.currentTimeMillis() - msStart;
                                    msStart = 0; // stopped
                                    tRepainter.suspend(); // we don't need to be repainting frequently
                                    _updateCounter();
                            }
                    }
                    else if( e.target == reset )
                    {
                            msTotal = 0;
                            if( 0 != msStart ) // started, then keep counting from 0
                            {
                                    msStart = System.currentTimeMillis();
                            }
                            _updateCounter();
                    }
                    return true; // we've handled the message
            default:
                    return false; // the message has not yet been handled
            }
    }        // static 'main' is the starting function for applications
    public static void main(String args[]) {
            // In an application, we don't automagically get a frame from the browser
            Frame f = new Frame("Stopwatch");
            // and the browser doesn't instantiate our applet, so we have to make our
            // own 'this'
            Stopwatch Stopwatch = new Stopwatch();
            // do the Applet-style initialization
            Stopwatch.init();
            // and start going...
            Stopwatch.start();        // add the Stopwatch to the frame we created
            f.add("Center", Stopwatch);
            f.resize( Stopwatch.size() );
            // display the frame
            f.show();
    }        // take care of updating the display with the appropriate string
    protected void _updateCounter() {
            long seconds;
            if( 0 != msStart )
            {
                    seconds = (  msTotal + System.currentTimeMillis() - msStart ) / 1000;
            }
            else
            {
                    seconds = msTotal / 1000;
            }
            counter.setText( Long.toString( seconds ) + " sec" );
            getLayout().layoutContainer( this );
    }
    } // class Stopwatch
    // class to continually repaint
    class Repainter implements Runnable {
            Stopwatch applet;
            boolean fExit;        // On construction, we take the applet we should repaint
    public Repainter( Stopwatch applet ) {
            this.applet = applet;
            fExit = false; // we're not done yet
    }public void exit() {
            fExit = true; // set the flag that we are done
    }        // while running, update the display every second
            // we could update it much more frequently, but that eats too much CPU
    public void run() {
            while( !fExit )
            {
                    applet._updateCounter();
                    try {
                            Thread.sleep( 1000 ); // wait 1 second...
                    }
                    catch( Exception e ) {
                            // we don't really care about the exception...
                    }
            }
    }
    } // class Repainter
      

  5.   

    首先,有一点,如果你的时间要想变,有两种方法,就是不停的date=Calendar.getInstance(),也就是说,需要在update()中加入这一句;第二,让你的时间累加。第二种方法有可能会带来运行时误差(此句属于个人见解)。我采用第一种方法,但发现后来g.drawString()的内容会盖住原来的内容,所以我在g.drawString()之前加入了g.clearRect(0, 0, 100, 100);一句。虽然有些不好看,但问题算是解决了。import javax.swing.*;
    import java.util.*;
    import java.awt.*;public class Timer extends JApplet implements Runnable{
    private Thread thr;
    private Calendar date;
    private String s;
    private JLabel l;

    public void run(){
    Thread me = Thread.currentThread();
    while(thr==me){
    try{
    Thread.currentThread().sleep(100);
    }catch(InterruptedException e){
    System.out.println("Interrupted");
    }
    repaint();
    }
    }

    public void update(Graphics g){
    date=Calendar.getInstance();
    s=date.get(Calendar.HOUR)+":"
    +date.get(Calendar.MINUTE)+":"+date.get(Calendar.SECOND);
    g.clearRect(0, 0, 100, 100);
    g.drawString(s,10,15);
    }

    public void init(){
    l=new JLabel();
    getContentPane().add(l);

    }

    public void start(){
    thr=new Thread(this);
    thr.start();
    }

    public void stop(){
    thr=null;
    }
    }
      

  6.   

    // g.clearRect(0, 0, 100, 100);
    // g.drawString(s,10,15);
    l.setText(s);
    我觉得这样似乎更好一点。(但需要使用布局管理器或setBound()把这个JLabel的位置控制一下)如下列代码:
    public void init(){
    l=new JLabel();
    getContentPane().add(l, BorderLayout.NORTH);
    }