Thread.sleep异常(int eclipse)

时间:2016-02-21 14:33:01

标签: java eclipse

我已经制作了一个带GUI的简单类型的cookie点击器,并希望制作CPS(每秒Cookie数),但即使有例外,thread.sleep也不起作用。

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();
                    }
                }

            }
        }
    }

}

1 个答案:

答案 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
    }
}

当然,您需要添加此处未包含的错误处理。