如何在延迟达到限制之前反复更新JLabel的延迟时间?

时间:2014-05-31 06:42:08

标签: timer delay jlabel sleep invokelater

我正在尝试用当前的旋转轮数更新我的JLabel,延迟越来越大,代表减速轮。我已经尝试过Thread.sleep(),Timer和invokeLater(),但我必须做一些非常错误的事情,因为我在过去几天尝试过的任何工作都没有。 Thread.sleep导致GUI只更新最后一件事,Timer忽略延迟并在旋转完成之前打印结果,我无法让invokeLater()更新JLabel。我该怎么办?

public void spin(int wheelSize, int initialDelay, int finalDelay, int delayIncrement, WheelCallback callback) {

        /**
         * Begins the spin on a random number within the wheelSize
         */
        Random randomNum = new Random();
        int currentNum = randomNum.nextInt((wheelSize - 0) + 1) + 0;
        //frame.setLabel(Integer.toString(currentNum));
        System.out.println(currentNum);
        /**
         * Loops the wheel until the finalDelay is met
         */
        for(int i = initialDelay; i < finalDelay; i += delayIncrement) {
                /**
                 * Causes the delay between numbers
                 */
            try {
                   Thread.sleep(i);
                }
                    catch(InterruptedException e) {
                }

                /**
                 * Increments the number to the next number up on the wheel.
                 * If the number reaches the maximum, it is reset to the minimum
                 */
                currentNum++;
                if(currentNum > wheelSize) {
                    currentNum = 0;
                }

                callback.nextNumber(currentNum, this);
            }       
            callback.result(currentNum, this);
            calculateResult(currentNum);
    }




public void nextNumber(int nextNumber, GameEngine engine) {
        frame.setLabel(Integer.toString(nextNumber));
    }

1 个答案:

答案 0 :(得分:0)