0

我正在尝试使用旋转轮的当前数量更新我的 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));
    }
4

1 回答 1

0

您可以使用Swing Timer,如该线程中所述:如何每次使用带有延迟的 while 循环更新 jLabel

于 2014-05-31T07:10:04.823 回答