2016-02-21 27 views
0

我已经制作了一个简单类型的Cookie图标,并且希望创建一个CPS(每秒Cookies数量),但thread.sleep不起作用,即使出现异常。Thread.sleep异常(int eclipse)

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.util.*; 


public class clicker extends JFrame implements ActionListener { 
    int w = 1; 
    int c = 0; 
    int u1 = 0; 
    int u2 = 0; 
    int u = 0; 
    JButton button1; 
    JButton button2; 
    JButton button3; 
    JLabel label1; 
    JLabel label2; 
    JLabel label3; 
    JPanel panel; 

    public clicker() { 

     this.setTitle("ActionListener Beispiel"); 
     this.setSize(400, 200); 
     panel = new JPanel(); 


     label1 = new JLabel(); 
     label2 = new JLabel(); 
     label3 = new JLabel(); 


     button1 = new JButton("Cookie"); 
     button1.setToolTipText("Click me!"); 
     button2 = new JButton("10c"); 
     button2.setToolTipText("+1 Cookie per Click"); 
     button3 = new JButton("20c"); 
     button3.setToolTipText("plus 3 Cookies per Click"); 


     button1.addActionListener(this); 
     button2.addActionListener(this); 
     button3.addActionListener(this); 


     panel.add(button1); 
     panel.add(label1); 
     panel.add(button2); 
     panel.add(label2); 
     panel.add(button3); 
     panel.add(label3); 



     getContentPane().add(panel); 
    } 

    public static void main(String[] args) { 

     clicker bl = new clicker(); 
     bl.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 

     if (ae.getSource() == this.button1) { 
      u = 1 + u1; 
      c = c + u; 
      label1.setText(" " + c); 
     } else if (ae.getSource() == this.button2 && c >= 10) { 
      u1++; 
      c = c - 10; 
      label2.setText(" " + u1); 
      label1.setText(" " + c); 

     } else if (ae.getSource() == this.button3 && c >= 20) { 
      u2++; 
      c = c - 20; 
      label3.setText(u2 + " CPS"); 
      label1.setText(" " + c); 
      c = c + u2; 
      while (true) { 
       if (u2 >= 1) { 
        c = c + u2; 
        label1.setText(" " + c); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
// TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

      } 
     } 
    } 

} 
+0

编辑过吧。它的最后while循环 – chrisi

+0

什么Thread.sleep不起作用?你在睡觉时是否期待GUI更新? –

+0

我只是缩进代码,使其可读性,我没有改变功能。 –

回答

0

你不能睡在GUI线程上,并期望GUI更新。一个简单的方法是使用javax.swing.Timer。下面是一些很粗糙的代码,以帮助入门:

import javax.swing.Timer; 

声明定时器与其他成员变量

... 
JPanel panel; 
Timer timer; 

沿...以及启动和处理定时器,而不是睡觉

... 
    label3.setText(u2 + " CPS"); 
    label1.setText(" " + c); 
    c = c + u2; 
    timer = new Timer(1000, this);  // Start a new timer that calls 
    timer.start();      // actionPerformed when it expires. 
} else if (ae.getSource() == timer) { // Handle timer events 
    if (u2 >= 1) { 
     c = c + u2; 
     label1.setText(" " + c); 
     timer.start();     // Restart the timer 
    } 
} 

当然,你会想要添加错误处理,这里不包括。

+0

它的工作。 YOu是真正的MVP – chrisi

+0

@chrisi如果答案有帮助,您可能需要将其标记为回答您的问题。这样可以帮助更多的人寻找类似的问题。 –