有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java线程。int睡眠异常(eclipse)

我制作了一个带有GUI的简单饼干点击器,想制作一个CPS(每秒饼干数),但不是线程。睡眠不起作用,即使有例外

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) 个答案