1、代码如下/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */package timer1;/**
 *
 * @author Administrator
 */
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class HandleTime extends JFrame{JLabel lbl=new JLabel();
Date now=new Date();
public HandleTime() {now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);Timer timer=new Timer(1000,new ActionListener(){public void actionPerformed(ActionEvent e) {Date now2=new Date(now.getTime()+1000);
now=now2;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");lbl.setText(formatter.format(now));
}});
timer.start();this.setLayout(new FlowLayout());
this.add(lbl);this.setSize(300,200);
this.setVisible(true);
}public static void main(String[] args) {
HandleTime t=new HandleTime();
}
} 我想设一个按钮,按下按钮,屏幕计时停止。
可是怎么试都不成功。
请问该如何操作?ps主要用于显示,一个程序的运行时间。比如,连接数据库之后,开始计时,屏幕可以实时显示00:00:00--->00:00:01,就像秒表一样,程序结束之后,这个秒表停止,屏幕显示总共的运行时间。
2、还有一个小问题,是识别文件后缀的。
我用filename.endsWith(".0A1")来判断,但是总是说,找不到该文件。如果重新复制粘贴一下该文件,就能找到了。这是为什么啊?

解决方案 »

  1.   

    那个识别后缀名的,用filename.trim().endsWith("XXXX")试试看
      

  2.   

    代码没看,计时停止的直接用Timer.stop()吧。
      

  3.   

    你的timer 别在构造方法中定义,这样就可以调用了,另外注意你的代码格式,很难看的
      

  4.   


    /**
     *
     * @author Administrator
     */
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Time;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    import javax.swing.JButton;
    public class HandleTime extends JFrame
    {
    JLabel lbl=new JLabel();
        JButton jButton = new JButton("stop");
        JButton jButton2 = new JButton("restart");    
        Timer timer;    Date now=new Date();
        public HandleTime() {
      
        now.setHours(0);
        now.setMinutes(0);
        now.setSeconds(0);
        timer=new Timer(1000,new ActionListener(){    public void actionPerformed(ActionEvent e) 
       {
        Date now2=new Date(now.getTime()+1000);
        now=now2;
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");    lbl.setText(formatter.format(now));
       } 
       });
        timer.start();
        
        jButton.addActionListener(new ActionListener(){
    public  void actionPerformed(ActionEvent e)
    {  
      timer.stop();
    }
    });
        jButton2.addActionListener(new ActionListener(){
    public  void actionPerformed(ActionEvent e)
    {   
      timer.restart();
    }
    });
        this.setLayout(new FlowLayout());
        this.add(lbl);
        this.add(jButton);
        this.add(jButton2);    this.setSize(300,200);
        this.setVisible(true);
    }
    public static void main(String[] args)
    {
        HandleTime t=new HandleTime();
    }