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

时间:2017-03-01 18:09:25

标签: 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 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

0 个答案:

没有答案