将私有变量传递给java中的另一个私有函数

将私有变量传递给java中的另一个私有函数,java,Java,我正试着自己做秒表,但我遇到了一些麻烦,关于如何停止它,因为按钮是私人的,我不能从停止按钮访问它 这是我的时钟课: public class initClock { int h; int m; int s; public initClock(int h1, int m1, int s1){ h = h1; m = m1; s = s1; } } 这是我的秒表课: public class Stopwatc

我正试着自己做秒表,但我遇到了一些麻烦,关于如何停止它,因为按钮是私人的,我不能从停止按钮访问它

这是我的时钟课:

public class initClock {
    int h;
    int m;
    int s;

    public initClock(int h1, int m1, int s1){
        h = h1;
        m = m1;
        s = s1;
    }
}
这是我的秒表课:

public class Stopwatch {
    JTextField upField;
    int flag;
    initClock clock = new initClock(0,0,0);
    Timer runTime = new Timer();
    TimerTask task = new TimerTask(){
        public void run(){
            if(clock.s == 60){
                clock.m++;
                clock.s = 0;
            }else if(clock.m == 60){
                clock.h++;
                clock.m=0;
            }
            clock.s++;
            upField.setText(clock.h + ":" + clock.m + ":" + clock.s);
        }
    };

    public Stopwatch(JTextField field){
        upField = field;
    }
}
这就是我按下按钮时的启动方式:

private void bStart1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
 Stopwatch clock1 = new Stopwatch(tfStopwatch1);
 clock1.runTime.scheduleAtFixedRate(clock1.task, 1000, 1000);}   
有没有办法让它停下来

比如:

private void bStop1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
  getClockFromStart().cancel();}

Thx=D

clock1
不是私有的-它是函数的本地。除非您正在谈论其他内容,在这种情况下,您需要更好地突出显示抱歉我的错误,我正在尝试访问BSTOP1 ActionPerformed上的时钟1,因此,我可以在单击按钮时停止时钟。在
bStart1ActionPerformed
方法之外声明
clock1
,这样您就可以从
bStart1ActionPerformed
方法之外访问它,允许您从其他方法调用
cancel()
。只需取消计时器(
runTime
)。但是您的代码中有几个主要的缺点。首先,不能保证每次以1000毫秒的准确间隔调用计时器任务;因此,你的秒表可能坏了。其次,您应该使用
System.nanotime()
,它不受人们更改系统时钟的影响。Klitos,如果我无法访问它,我如何取消运行时=/?我将更多地了解您的建议以及如何实施,谢谢!