首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于秒表计时器不按开始旋钮,直接计时

2012-12-17 
关于秒表计时器不按开始按钮,直接计时。如图:我见到的都是点击开始按钮,才开始计时的计时器,哪位好心人帮我

关于秒表计时器不按开始按钮,直接计时。
如图:


我见到的都是点击开始按钮,才开始计时的计时器,哪位好心人帮我写个如图所示的不点击按钮,运行直接开始计时的秒表计时器啊,谢谢了!
[最优解释]


public class StopWatch extends JFrame {
JTextArea timeArea; // 时间显示
JButton startBtn, pauseBtn, resetBtn;
private int mins = 0, sec = 0;
private long currentTime; // 记录系统当前时间。
private long difference; // 秒表记录并要显示的时间。
private long timeOfStart; // 记录按下"开始"时的系统时间。
private long timeLengthOfPause; // 按下"暂停"时记录累计计时时间。
private String timeStr, str1, str2;

public StopWatch(String str) {
super(str);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
timeArea = new JTextArea("", 1, 12);
timeArea.setText("00:00");
new Thread(new Runnable() {
public void run() {
while(true){
currentTime=System.currentTimeMillis();        //当前时间
            difference=timeLengthOfPause+(currentTime-timeOfStart);    //计时时间。
            difference=difference/10;            //以10ms为单位。
            mins=(int)(difference/(60*100));        //分钟数
            sec=(int)((difference/100)%60);            //秒数
          
            str1=(mins<10)?("0"+mins+":"):(mins+":");
            str2=(sec<10)?("0"+sec):(sec+"");
  
            timeStr=str1+str2;
            timeArea.setText(timeStr);
}
}
}).start();
contentPane.add(timeArea, BorderLayout.NORTH);
// 中间:启动和暂停按钮
JPanel pCenter = new JPanel(new GridLayout(1, 2));
startBtn = new JButton("开始");
pCenter.add(startBtn);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口退出程序。
}

public static void main(String args[]) {
new StopWatch("秒表");
}
}


至于的你时间计算是怎么回事,我不太清楚你的需求,我借用的时间生成的代码……
[其他解释]
怎么做都会有触发条件吧 只不过位置不一样而已
[其他解释]
我的意思就是想不监听启动就计时,
              btn.addActionListener(this);
              public void actionPerformed(ActionEvent e){
                  if(e.getSource()==btn){...}
              }
我不要用这样的点击按钮监听才计时,我要启动计时,求求你们了,给我写个吧。
------其他解决方案--------------------


F5都快刷烂了,求求你们了,可怜可怜我吧。
[其他解释]
写个线程,窗体初始化就开始run!
[其他解释]
楼上大哥说用线程,我觉得是可以的,可我不会具体实现,以下面的程序为准怎么修改,用线程的方式,让程序启动就计时?谢谢大家!


import java.awt.BorderLayout;
public class StopWatch extends JFrame implements ActionListener
{
    JTextArea timeArea; //时间显示
    JButton startBtn,pauseBtn,resetBtn;
    private int mins=0,sec=0;
    private long currentTime;                            //记录系统当前时间。
    private long difference;                            //秒表记录并要显示的时间。
    private long timeOfStart;                            //记录按下"开始"时的系统时间。
    private long timeLengthOfPause;                            //按下"暂停"时记录累计计时时间。
    Timer timer;
    private String timeStr,str1,str2;
    public StopWatch(String str)
    {
        super(str);
        Container contentPane=getContentPane();
        contentPane.setLayout(new BorderLayout());
        timeArea=new JTextArea("",1,12);
        timeArea.setText("00:00");
        contentPane.add(timeArea,BorderLayout.NORTH);
        //中间:启动和暂停按钮
        JPanel pCenter=new JPanel(new GridLayout(1,2));
        startBtn=new JButton("开始");
        pCenter.add(startBtn);
        startBtn.addActionListener(this);
        timer=new Timer(10,this);            //设定定时器周期为10毫秒
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //关闭窗口退出程序。
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==timer)
        {
            currentTime=System.currentTimeMillis();        //当前时间
            difference=timeLengthOfPause+(currentTime-timeOfStart);    //计时时间。
            difference=difference/10;            //以10ms为单位。


            mins=(int)(difference/(60*100));        //分钟数
            sec=(int)((difference/100)%60);            //秒数
          
            str1=(mins<10)?("0"+mins+":"):(mins+":");
            str2=(sec<10)?("0"+sec):(sec+"");
  
            timeStr=str1+str2;
            timeArea.setText(timeStr);
        }
       
        if(e.getSource()==startBtn))
        {
            timeLengthOfPause=0;
            timeOfStart=System.currentTimeMillis();        //按开始的时候的时刻记录.
            timer.start(); 
           
        }
   }
    public static void main(String args[])
    {
        StopWatch s=new StopWatch("秒表");
    }
}


[其他解释]
实现不行大伙用线程给我写个吧,拜托了
[其他解释]
这个代码好像眼熟,我曾经写过一个,现在改改看是否符合楼主的要求.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class StopWatch3 extends JFrame implements ActionListener
{
JTextArea timeArea; //时间显示
JButton startBtn,pauseBtn,resetBtn;
private int mins=0,sec=0,mSec=0;
private long currentTime;//记录系统当前时间。
private long difference;//秒表记录并要显示的时间。
private long timeOfStart;//记录按下"开始"时的系统时间。
private long timeLengthOfPause;//按下"暂停"时记录累计计时时间。
Timer timer;
private String timeStr,str1,str2,str3;
private boolean first=true;//第一次按下后,first=false.
private boolean start=false;//"开始"按下后,start=true.
private boolean pause=false;//"暂停"按下后 pause=true.

public StopWatch3(String str)
{
super(str);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());

//北边:时间显示区域
timeArea=new JTextArea("",1,12);
timeArea.setFont(new Font("隶书",Font.ITALIC,24));
timeArea.setText("00:00:00");
contentPane.add(timeArea,BorderLayout.NORTH);
//中间:启动和暂停按钮
JPanel pCenter=new JPanel(new GridLayout(1,2));
startBtn=new JButton("开始");
pCenter.add(startBtn);
startBtn.addActionListener(this);
pauseBtn=new JButton("暂停");
pCenter.add(pauseBtn);
pauseBtn.addActionListener(this);
contentPane.add(pCenter,BorderLayout.CENTER);
//南边:复位按钮
resetBtn=new JButton("复位");
resetBtn.addActionListener(this);
contentPane.add(resetBtn,BorderLayout.SOUTH);



timer=new Timer(10,this);//设定定时器周期为10毫秒

pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口退出程序。
//程序一运行就计时.(这块就是一开始就计时).
timeOfStart=System.currentTimeMillis();//按开始的时候的时刻记录.
timer.start();
first=false;
start=true;
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==timer)
{
currentTime=System.currentTimeMillis();//当前时间

difference=timeLengthOfPause+(currentTime-timeOfStart);//计时时间。
difference=difference/10;//以10ms为单位。
mins=(int)(difference/(60*100));//分钟数
sec=(int)((difference/100)%60);//秒数
mSec=(int)difference%100;//1(10)毫秒数。

//显示新时间
str1=(mins<10)?("0"+mins+":"):(mins+":");
str2=(sec<10)?("0"+sec+":"):(sec+":");
str3=(mSec<10)?("0"+mSec):(mSec+"");

timeStr=str1+str2+str3;
timeArea.setText(timeStr);
}
//只有在开始时或者复位后,或按下暂停键才起作用。正在计时时按此键不起作用。
//
if(e.getSource()==startBtn&&(first
[其他解释]
pause))
{
start=true;//设置已经启动标志。
pause=false;//清除暂停标志。
if(first)//开始第一次或复位后
{//按"开始"才执行。
timeLengthOfPause=0;
first=false;
}
timeOfStart=System.currentTimeMillis();//按开始的时候的时刻记录.
timer.start();//启动计时。
}

//只有在计时时按此键才有效。否则无效。
//
if(e.getSource()==pauseBtn&&start)
{
pause=true;//设置暂停标志。
start=false;//清除启动标志。
timeLengthOfPause=mins*60*1000+sec*1000+mSec*10;   //暂停后记录已累计计时的毫秒数。
timer.stop();//停止计时。
}

//任何时候都可以复位。
//
if(e.getSource()==resetBtn)
{
mins=0;
sec=0;
mSec=0;
timer.stop();
first=true;
start=false;
timeLengthOfPause=0;//开始时以前累计的时间清零。
timeArea.setText("00:00:00");//显示区清零。
}
}

public static void main(String args[])
{
StopWatch3 s=new StopWatch3("秒表--stopwatch");
}
}


[其他解释]

public class StopWatch extends JFrame {
JTextArea timeArea; // 时间显示
JButton startBtn, pauseBtn, resetBtn;
private int mins = 0, sec = 0;
private long currentTime; // 记录系统当前时间。
private long difference; // 秒表记录并要显示的时间。
private long timeOfStart; // 记录按下"开始"时的系统时间。
private long timeLengthOfPause; // 按下"暂停"时记录累计计时时间。
private String timeStr, str1, str2;

public StopWatch(String str) {
super(str);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
timeArea = new JTextArea("", 1, 12);
timeArea.setText("00:00");
new Thread(new Runnable() {
public void run() {
while(true){
currentTime=System.currentTimeMillis();        //当前时间
            difference=timeLengthOfPause+(currentTime-timeOfStart);    //计时时间。
            difference=difference/10;            //以10ms为单位。
            mins=(int)(difference/(60*100));        //分钟数


            sec=(int)((difference/100)%60);            //秒数
          
            str1=(mins<10)?("0"+mins+":"):(mins+":");
            str2=(sec<10)?("0"+sec):(sec+"");
  
            timeStr=str1+str2;
            timeArea.setText(timeStr);
}
}
}).start();
contentPane.add(timeArea, BorderLayout.NORTH);
// 中间:启动和暂停按钮
JPanel pCenter = new JPanel(new GridLayout(1, 2));
startBtn = new JButton("开始");
pCenter.add(startBtn);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口退出程序。
}

public static void main(String args[]) {
new StopWatch("秒表");
}
}



至于的你时间计算是怎么回事,我不太清楚你的需求,我借用的时间生成的代码……
[其他解释]
就楼主的代码,直接把

            timeLengthOfPause=0;
            timeOfStart=System.currentTimeMillis();        //按开始的时候的时刻记录.
            timer.start(); 
           

放在构造函数里,即可实现.

public StopWatch(String str)
    {
        super(str);
        Container contentPane=getContentPane();
        contentPane.setLayout(new BorderLayout());
        timeArea=new JTextArea("",1,12);
        timeArea.setText("00:00");
        contentPane.add(timeArea,BorderLayout.NORTH);
        //中间:启动和暂停按钮
        JPanel pCenter=new JPanel(new GridLayout(1,2));
        startBtn=new JButton("开始");
        pCenter.add(startBtn);
        startBtn.addActionListener(this);
        timer=new Timer(10,this);            //设定定时器周期为10毫秒
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //关闭窗口退出程序。
         //加在这
         timeLengthOfPause=0;
        timeOfStart=System.currentTimeMillis();        //按开始的时候的时刻记录.
        timer.start(); 



热点排行