如何让方法等到定时器代码执行完毕?

时间:2016-04-18 21:00:30

标签: java swing timer javax.swing.timer

public void highlightThisText(){

    int delay = 1000;
    ActionListener tp = new ActionListener(){

        public void actionPerformed(ActionEvent ae){
            try{
                System.out.println();
                if(name.equals("Correct")){
                    thisPanel.getHighlighter().addHighlight(startIndex, endIndex, hl);
                }
                else{
                    anotherPanel.getHighlighter().addHighlight(startIndex, endIndex, hl);
                }

            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }
    };
    Timer t = new Timer(delay, tp);
    t.setRepeats(false);
    t.start();      
}

我需要我的程序进入highlightThisText方法,执行定时器代码,并在延迟之后离开方法。我明白我不能使用Thread.sleep(1000);因为这会阻止EDT,但我在这里找不到任何其他类似问题的例子。方法离开后,startIndex和endIndex将递增,这意味着不会突出显示正确的行。

任何帮助都将受到高度赞赏。

0 个答案:

没有答案
相关问题