2016-05-30 48 views
1

我正在爲uni任務做一個撲克遊戲,我想知道是否有任何方法有如下方法(注意:非常粗略的代碼)任何方式來延遲在Java GUI中的操作

JTextArea textArea = new JTextArea(); 

public void printer(String s){ 
    //I want to delay here, for 2 seconds each time i print to the jtextarea 
    textArea.append(s); 
} 

public void runGame(){ 
    printer("Dealing cards..."); 
    //I want to delay to add to an effect of the time it takes to actually deal the cards 
    pokerHand.setVisibility(true); 
    printer("What you like to do?"); 

    // 
    //Code here containing running the game 
    // 

    printer("Daniel Negreanu folds"); 
    //I want to have a delay here for the time it takes to make a decision. 
    printer("Phil Hellmuth folds"); 

Theres許多更多的實例,我想在整個程序中使用它,只是想知道是否有任何方法可以做到這一點。

在此先感謝

編輯:不希望使用Thread.sleep()方法,因爲它不貴很好地工作。 編輯2:我想pokerHand.setVisibility(true)和其他方法在我的代碼中執行延遲後(使用定時器不執行此操作)。

+0

Thread.sleep(2000)將整個gui暫停2秒,稍微凍結它,它不是我正在尋找,編輯的內容。無論如何,謝謝 –

+0

@ cricket_007他們使用的方法會延遲消息,但不會延遲其他代碼的運行,例如,計時器將暫停printer()方法,pokerHand.setVisibility(true)會毫無延遲地執行。 –

+0

然後,您在代碼 –

回答

4

不希望使用Thread.sleep(),因爲它不適用於gui。

好,使用一個Swing Timer代替

我想pokerHand.setVisibility(真),以及其他方法在我的代碼執行的延遲之後,(使用定時器不這樣做)。

是這樣做,你只是不使用它正確,但因爲你沒有提供任何實際的代碼,我可以「怎麼樣」你不使用得當,僅從聲音不說它是,你是。

開始採取看看How to use Swing Timers更多細節

下面是很簡單的例子,它採用了Timer來計算和打印您單擊按鈕的時間之間的時間量。

的例子更新之後的Timer啓動的UI,但直到Timer完成,不產生計算和結果

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.time.Duration; 
import java.time.LocalTime; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JTextArea ta;  
     private JButton btn; 

     private Timer timer; 

     private LocalTime startTime, endTime; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      ta = new JTextArea(10, 20); 
      add(new JScrollPane(ta)); 
      btn = new JButton("Click"); 
      add(btn, BorderLayout.SOUTH); 

      timer = new Timer(2000, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        endTime = LocalTime.now(); 
        Duration duration = Duration.between(startTime, endTime); 
        ta.append("Ended @ " + endTime + "\n"); 
        ta.append("Took " + (duration.toMillis()/1000) + " seconds\n"); 
       } 
      }); 
      timer.setRepeats(false); 

      ta.setEditable(false); 

      btn.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        timer.start(); 
        startTime = LocalTime.now(); 
        btn.setEnabled(false); 
        ta.append("Start @ " + startTime + "\n"); 
       } 
      }); 
     } 

    } 

} 
+0

感謝你:-)正在使用他們錯了 –

1

首先,你的代碼似乎是勢在必行(「詢問用戶該怎麼辦「,」遊戲循環在這裏「),而不是事件驅動(」當發生這種情況,那樣做「)。在所有GUI上使用的event-driven programming,請儘快閱讀。

在事件驅動的代碼中,「用戶單擊按鈕」和「超時過期」之間沒有太大的區別 - 兩者都會導致您的代碼可以反應的事件。因此,首先重寫您的代碼,以便一旦按下按鈕,就會發生「下一件事」。然後再次更改它,以便一旦計時器到期,就會發生相同的「下一件事」。

假設你有下面的代碼:

// an ActionListener for both clicks and timer-expired events 
private nextThingListener = new ActionListener() { 
    public void actionPerformed(ActionEvent ae) { 
     doNextThing(); 
    } 
}; 

// method that does the next thing 
public void doNextThing() { 
    // change the UI in some way 
    // if using timers, may also start the next timer 
} 

現在,您可以用它關聯到按鈕:

// create a button and have it do the "next thing" when clicked 
JButton jb = new JButton("do next thing"); 
jb.addActionListener(nextThingListener); 
myInterface.add(jb); // <-- display it in the interface somehow 

你可以到一個計時器,因爲它關聯:

// launch a timer that will ring the nextThingListener every 2 seconds 
new Timer(2000, nextThingListener).start(); 

如果你想讓定時器只響一次,你可以使用

// launch a timer that will ring the nextThingListener after 2 seconds 
Timer t = new Timer(2000, nextThingListener); 
t.setRepeats(false); 
t.start();