用循环定时器重复java的第一个结果?

时间:2015-11-26 13:54:01

标签: java loops timer

我多次尝试使用此代码,使字母在字母 之间等待4秒后打印字母,只打印第一个字母然后重复所有字母时间 !

 StatusBar.getInstance().ShowMoves.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
         StatusBar.getInstance().ShowMoves.setEnabled(false);
         sol=null;
         sol = StatusBar.getInstance().getSolution();
         Timer timer = new Timer(); 
         for (int tbn = 0; tbn < sol.length(); tbn++) { 
             temp = tbn ;

             timer.schedule(new TimerTask() {
                 @Override                                
                 public void run() {                        
                     switch (sol.charAt(temp)) {   
                         case 'u': 
                             StatusBar.getInstance().TimeTaken.setText(StatusBar.getInstance().TimeTaken.getText()+"u");
                             break;
                         case'd':
                             StatusBar.getInstance().TimeTaken.setText(StatusBar.getInstance().TimeTaken.getText()+"d");
                             break;
                         case'l':
                             StatusBar.getInstance().TimeTaken.setText(StatusBar.getInstance().TimeTaken.getText()+"l");
                             break;
                         case'r':
                             StatusBar.getInstance().TimeTaken.setText(StatusBar.getInstance().TimeTaken.getText()+"r");
                             break;
                         }        
                         try {
                            Thread.sleep(1000);
                         } catch (InterruptedException ex) {
                             Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
                         }
                     }}, 4000);
                 }
             }
         }
     );                                                                                               
     this.pack();

     KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(Controller.getInstance());
 }

代码有什么问题?

2 个答案:

答案 0 :(得分:0)

取自文件,参考资料如下:

代码:

public class Test {
    public static void main(String args[])
        throws InterruptedException {
        String importantInfo[] = {
            "Mares eat oats",
            "Does eat oats",
            "Little lambs eat ivy",
            "A kid will eat ivy too"
        };

        for (int i = 0;
             i < importantInfo.length;
             i++) {
            //Pause for 4 seconds
            Thread.sleep(4000);
            //Print a message
            System.out.println(importantInfo[i]);
        }
    }
}

<强>参考:
https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep%28long%29

答案 1 :(得分:0)

我通过将循环置于计时器而不是循环中的计时器来解决它

StatusBar.getInstance().ShowMoves.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    StatusBar.getInstance().ShowMoves.setEnabled(false);
                    sol=null;
                    sol = StatusBar.getInstance().getSolution();
                    Timer timer = new Timer(); 

                    timer.schedule(new TimerTask() {
                    @Override                                
                    public void run() {

                        for (int tbn = 0; tbn < sol.length(); tbn++)
                    { 
                    switch (sol.charAt(tbn))
                    {

                        case 'u':
                            GamePanel.getInstance().moveUp();
                    {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                            break;
                        case'd':
                        GamePanel.getInstance().moveDown();
                        {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                            break;
                        case'l':
                         GamePanel.getInstance().moveLeft();
                        {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                            break;
                        case'r':
                        GamePanel.getInstance().moveRight();
                        {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                            break;

                    }} 


                             }}, 1000);

                    }






                }


            );




    this.pack();

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(Controller.getInstance());
}