在while循环中使用Timer

时间:2017-06-10 09:54:55

标签: java multidimensional-array timer while-loop jlabel

我想在while循环中使用Timer。也就是说,我想在其中包含一个计时器时多次运行while循环体。但是,每当我执行此操作时,while循环的主体似乎在计时器触发所需的时间之前执行并递增。它应该在while循环递增之前触发15次,但它似乎在计时器触发所需的时间之前立即运行while循环,这是我不明白的。

我知道这不是其他任何问题,因为当我只运行一次代码(即没有while循环)时,它运行正常。这个想法是JLabel应该被移除并每1300ms更换一次,直到显示15个图像。一旦显示15个图像,它就会移动到2D阵列的下一行,其中总共有60行。

任何帮助将不胜感激。我把代码放在下面。

public class Game implements KeyListener, ActionListener{

    private Preparation prep;
    private Window window;
    private JLabel earthLabel;
    private JPanel panel;
    private Timer timer;
    private JLabel[] arrayOfAllSpaceshipsLabels;
    private int[] arrayOfAllSpaceshipsIndexes;
    private JLabel currentShipLabel;
    private int numberOfBlocks;
    private int firings;
    private int[][] blocks = new int[60][15] //the values in this array have been initalized in another class that is too big and not relevant to my problem

    public Game(){
        window = new Window("Space Game"); 
        runBlock();

    }

    private void runBlock() {
        int blocksLength = blocks.length;
        numberOfBlocks = 0;
        while(numberOfBlocks < blocksLength){
            firings = 0;

            timer = new Timer(1333, new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {

                    if (firings == 15){
                        panel.removeAll(); 
                        panel.revalidate();
                        panel.repaint();
                        timer.stop();
                        System.out.println("Block finished");
                        return;
                    }
                    panel.removeAll(); /*(currentShipLabel);*/
                    panel.revalidate();
                    panel.repaint();

                    currentShipLabel = arrayOfAllSpaceshipsLabels[blocks[numberOfBlocks][firings]];
                    panel.add(currentShipLabel);
                    panel.validate();

                    firings++;

                }
            });
            timer.start();
        }
        numberOfBlocks++;
    }
}

public class Window extends JFrame{

    private JPanel panel;

    public Window(String title){
        SwingUtilities.isEventDispatchThread();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setBackground(Color.BLACK);
        panel = new JPanel();
        this.add(panel);
        this.pack();
        this.setSize(500,500); //my edit
        panel.setBackground(Color.BLACK);
        this.setVisible(true);

    }

    public JPanel getPanel() {
        // TODO Auto-generated method stub
        return panel;
    }

}

1 个答案:

答案 0 :(得分:2)

正如您所注意到的,创建和启动Timer不会阻止循环继续。

要解决您的问题,您必须将while循环“移动”到计时器操作中:

private void runBlock() {
    int blocksLength = blocks.length;
    numberOfBlocks = 0;
    timer = new Timer(1333, new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

            if (firings == 15){
                if (numberOfBlocks == blocksLength)
                    panel.removeAll(); 
                    panel.revalidate();
                    panel.repaint();
                    timer.stop();
                    System.out.println("Block finished");
                    return;
                } else {
                   firings = 0;
                   numberOfBlocks++;
                }
            }
            panel.removeAll(); /*(currentShipLabel);*/
            panel.revalidate();
            panel.repaint();

            currentShipLabel = arrayOfAllSpaceshipsLabels[blocks[numberOfBlocks][firings]];
            panel.add(currentShipLabel);
            panel.revalidate();

            firings++;

        }
    });
    timer.start();
}