如何在点击屏幕10分钟后让我的计时器结束?

时间:2015-01-22 22:36:53

标签: java swing timer timertask

目前我的计时器在程序启动后10分钟后结束。我希望它在用户点击开始按钮10分钟后结束。我知道它是因为我将开始设置为System.currentTimeMillis(),但我不知道还有什么设置它。我已经坚持这么久了。我对Java很陌生,所以我会尽可能多地了解特殊性和外行语言。谢谢你的帮助。

我不知道我是否可以加粗代码,但我加粗了代码中需要更改的相关部分,以便更容易找到(所以这里有一个星号或者它是&#39 ;对这些部分大胆)。

如果它有帮助,我试图创建一个程序,产生10分钟的随机数节拍,然后在10分钟后在屏幕上显示最终节拍数。

计时器类:

package com.home.timer;

import java.util.Timer;
import java.util.TimerTask;

public class MyTimer extends TimerTask
{
    Timer timer;
    int count = 0;
    public MyTimer()
    {   
    }

    public MyTimer (Timer timer)
    {
        this.timer = timer;
    }

    public void toDo()
    {
        System.out.println (" Count: " + (count++));
    }

    **long start = System.currentTimeMillis();
    long end = start + 10*1000;**

    @Override
    public void run()
    {
        toDo();
        **if (System.currentTimeMillis() > end)//this is condition when you want to stop task
        {
            timer.cancel();
        }**
    }
}

任务类:

package com.home.timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.Timer;
import javax.swing.JPanel;

public class MyScheduler extends JPanel
{

public static final long serialVersionUID = 1L;

public static MyFrame d = new MyFrame();
public static MyTimer t = new MyTimer();

public Font large = new Font ("Calibri", Font.BOLD, 30);
public Font small = new Font ("Calibri", Font.PLAIN, 18);
public Rectangle mainMenu = new Rectangle (310,290,60,120);
public Rectangle beginButton = new Rectangle (310, 290, 650, 300);
public Rectangle beginNow = new Rectangle (594, 472, 80,40);
public Rectangle beginNow2 = new Rectangle (594,492,80,40); //new variable because beginNow isn't at right location for some reason
public static String appState = "Begin";

Timer timer = new Timer();
Random time = new Random();
int number = 2000+time.nextInt(3000); //setting period to random

MyTimer myTask = new MyTimer (timer);
MySound myBeat = new MySound (timer);

int firstStart = 1000; //timer will start after x ms
int period = number; //task will repeat after this period

public MyScheduler (MyFrame d)
{
d.f.addMouseListener(new MouseListener()
{
    public void mouseClicked(java.awt.event.MouseEvent arg0) //click button to begin task
    {
        Point p = arg0.getPoint();
        if (beginNow2.contains(p)){
            appState = "Start"; //Start = no dialogue box
            timer.schedule(myTask, firstStart, period); //begin count
            timer.schedule(myBeat,firstStart, period); //begin beats
        }
    }
    public void mouseEntered(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mouseExited(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mousePressed(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mouseReleased(java.awt.event.MouseEvent arg0)
    {   
    }
});
}

public static void atEnd() //if time is over, then appState changes to Result
{
    while(true){
    **if (System.currentTimeMillis() > t.end)//this is condition when you want to stop task
    {
        appState = "Result";
    }**
    }
}

public void paintComponent (Graphics g)
{
    super.paintComponent(g);

    g.drawImage(MyFrame.i.BG, 0, 0, MyScheduler.d.width, MyScheduler.d.height, null); //background picture

    if (appState == "Begin") //Begin = dialogue box
    {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(310,290,650,300);
        g.setColor(Color.WHITE);
        g.drawRect(beginButton.x, beginButton.y, beginButton.width, beginButton.height);
        g.setFont(large);
        g.drawString("Begin", beginButton.x +290, beginButton.y+210);
        g.drawRect(beginNow.x, beginNow.y, beginNow.width, beginNow.height);
    }

    if (appState == "Result") //shows count at end
    {
        g.setFont(large);
        g.setColor (Color.RED);
        g.drawString(myTask.count-1+" beats", 200, 200);
    }

    repaint();
}

public void startApp() //startApp = Start = toStart
{
    appState = "Start";
}
public void beginApp() //beginApp = Begin = toBegin
{
    appState = "Begin";
}
public void resultApp() //resultApp = Result = toResult
{
    appState = "Result";
}

public boolean toBegin()
{
    return (appState.equalsIgnoreCase("Begin")? true:false);
}
public boolean toStart()
{
    return (appState.equalsIgnoreCase("Start")? true:false);
}
public boolean toResult()
{
    return (appState.equalsIgnoreCase("Result")? true:false);
}

    public static void main(String [] args)
    {
        d.display();
        atEnd();
    }
}

修改

这是&#39>的样子

if (beginNow2.contains(p)) { 
    appState = "Start"; 
    myTask = new MyTimer(timer); 
    timer.schedule(myTask, firstStart, period);
    timer.schedule(myBeat,firstStart, period); 
    long start = System.currentTimeMillis(); 
    long end = start + 10*1000; 
    if (System.currentTimeMillis() > end) { 
        timer.cancel(); appState = "Result";
    } 
}

1 个答案:

答案 0 :(得分:0)

long start = System.currentTimeMillis();
long end = start + 10*1000;

MyTimer中的这些字段导致了问题。它们会在您运行MyTimer构造函数时(在程序开头)初始化。最简单的解决方案是在您想要开始倒计时之前创建新的MyTimer实例(点击Begin后)。即在schedule()电话之前:

...
myTask = new MyTimer(timer);
timer.schedule(myTask, firstStart, period);
...