我想在另一个 class 中使用我的计数器,但它显示为初始值零

I want to use my counter in a different class, but it appears as its initial value of zero

我正在尝试创建一个程序,该程序会在 5 分钟内生成随机节拍数,然后在 5 分钟后在屏幕上显示最终节拍数。这是与我当前问题相关的两个 classes。我已经尝试了很多东西,但最终节拍数仍然显示为零。我是 Java 的超级新手,所以我希望尽可能具体。提前致谢!

另外,我还想让用户在计时器结束后在屏幕上输入他们的猜测。有人可以指出我在正确的轨道上吗?我真的无法在网上找到任何关于我将如何去做的事情。我不想要一个对话框(除非我可以改变它的外观?)。

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 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"; //need Begin for background

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 (toBegin()) //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(t.count+" 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();
    }
}

这是我的计时器 class,我从中获取计数。计时器设置为 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();
        }
    }
}

您有两个 MyTimer class 实例。一个是静态的 - t - 另一个不是 - myTaskTimer 实际使用的实例是 myTask,但您使用另一个实例来打印计数 - g.drawString(t.count+" beats", 200, 200);。因此,打印的计数是 0.

也就不足为奇了

尝试g.drawString(myTask.count+" beats", 200, 200);