2015-10-15 79 views
-1

我正在重新創建一個非常差的版本的cookie答題器只是爲了測試我的技能。那麼我遇到了一個問題,我無法弄清楚如何每3秒重複一次循環。 主類代碼:雖然循環 - 定時

import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 

public class CookieClickerGame { 
    private static final int FRAME_WIDTH = 200; 
    private static final int FRAME_HEIGHT = 200; 

    public static void main(String[] args) { 
     JPanel panel = new JPanel(); 

     JFrame frame = new JFrame(); 
     JButton cookie = new JButton("Cookie"); 
     JButton grandma = new JButton("Grandma"); 
     JButton robot = new JButton("Robot"); 
     panel.add(cookie); 
     panel.add(grandma); 
     panel.add(robot); 
     frame.add(panel); 

      /*----------------------------------------/ 
     /   Buttons     /
     /----------------------------------------*/ 
     ActionListener listener = new CookieClicker(); 
     cookie.addActionListener(listener); 

     ActionListener listener2 = new CookieClicker(); 
     grandma.addActionListener(listener2); 

     ActionListener listener3 = new CookieClicker(); 
     robot.addActionListener(listener3); 

      /*---------------------------------------------------------/ 
     /      Frame       /
     /---------------------------------------------------------*/ 
     frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

監聽器代碼:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JOptionPane; 

public class CookieClicker implements ActionListener { 
    static int cookies; 
    public void actionPerformed(ActionEvent event) { 
     if (event.getActionCommand().equals("Cookie")) { 
      cookies++; 
      System.out.print('\f'); 
      System.out.println(cookies + " Cookies"); 
     } else if (event.getActionCommand().equals("Grandma") && cookies >= 10) { 
      System.out.println("+1 Grandma | -10 Cookies"); 
      cookies = cookies - 10; 
      System.out.println(cookies + " Cookies"); 
      while (true) { 
       cookies = cookies + 1; 
       System.out.print('\f'); 
       System.out.println(cookies + " Cookies"); 
       Thread.sleep(3000); 
       } 
     } else if (event.getActionCommand().equals("Robot") && cookies >= 50) { 
      System.out.println("+1 Robot | -50 Cookies"); 
      cookies = cookies - 50; 
      System.out.println(cookies + " Cookies"); 
      cookies = cookies + 1; 
      System.out.print('\f'); 
      System.out.println(cookies + " Cookies"); 
     } else { 
      JOptionPane.showMessageDialog (null, "Not Enough Cookies", "You're Poor", JOptionPane.INFORMATION_MESSAGE); 
      } 
     } 
    } 

Thread.sleep(3000)不工作,這有什麼錯呢?

+0

定義「不工作」。 – John3136

+0

請看[如何使用Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)[示例](http://stackoverflow.com/questions/ 18332135/java-for-loop-in-gui-textarea/18332315#18332315) – MadProgrammer

+0

用try..catch surrond它開始 –

回答

1

Swing是一個單線程框架,您不應該嘗試在Event Dispatching Thread的上下文中執行任何長時間運行或阻止操作。

看一看Concurrency in Swing更多細節

雖然你可以使用Thread來解決這個問題,一個簡單的解決方案是使用一個Swing Timer,因爲它是事件回調是事件的上下文中觸發調度線程,使其安全地從內

更多細節

喜歡的東西見How to use Swing Timers更新UI ...

} else if (event.getActionCommand().equals("Grandma") && cookies >= 10) { 
    System.out.println("+1 Grandma | -10 Cookies"); 
    cookies = cookies - 10; 
    System.out.println(cookies + " Cookies"); 
    if (timer == null || !timer.isRunning()) { 
     timer = new Timer(3000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       cookies = cookies + 1; 
       System.out.print('\f'); 
       System.out.println(cookies + " Cookies"); 
       // I don't know what your exit condition is, but you can use 
       // ((Timer)e.getSource()).stop(); 
       // to stop the timer from repeating 
      } 

     }); 
     timer.start(); 
    } 
} 

例如

+0

謝謝你! –