JButton Press上的线程抛出异常

时间:2015-12-19 03:34:35

标签: java multithreading runtime-error jbutton

所以我有这个程序,它是一种类似游戏的“Cookie Clicker”的基础,我已经设法弄清楚如何使它保持玩家所做的总点击次数的统计,以及闲置的点击器。但是,当我想通过单击JButton使程序每秒执行更多空闲点击时,除了Java抛出此错误。

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at counter_game.Counter$4.actionPerformed(Counter.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这就是程序的样子。它使用一个单独的类来运行程序中的添加。

public class Counter 
{

public static void main(String[] args) 
{
    System.out.print(" ");

    Modifiers runtime = new Modifiers();

    Font font = new Font("Veranda", Font.BOLD, 14);

    GridBagConstraints c = new GridBagConstraints();

    Container pane = new Container();
    pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    pane.setLayout(new GridBagLayout());


    JTextArea display = new JTextArea();
    display.setPreferredSize(new Dimension(700, 500));;
    display.setFont(font);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = .5;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    pane.add(display, c);

    Thread idleThread = new Thread()
    {
        public void run()
        {
            while(true)
            {
                runtime.idleClick();
                try
                {
                    Thread.sleep(1000);
                }catch(Exception c){}
                display.setText("Dollars:  $" + runtime.getTotal());
            }
        }
    };

    JButton clicker = new JButton();
    clicker.setText("Click Me!");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(0, 0, 10, 0);
    c.weightx = .5;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    clicker.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            runtime.clicked();
            display.setText("Dollars:  $" + runtime.getTotal());
        }
    });
    pane.add(clicker, c);

    JButton multiplier = new JButton();
    multiplier.setText("+1 $/click");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = .5;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    multiplier.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            runtime.multiply();
        }
    });
    pane.add(multiplier, c);

    JButton idle = new JButton();
    idle.setText("+1 $/sec");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = .5;
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    idle.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            runtime.idle();
            idleThread.start();
        }
    });
    pane.add(idle, c);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pane);
    frame.pack();
    frame.setVisible(true);

    }

}

{

public class Modifiers 

public Modifiers()
{
    total = 0;
    multiply = 0;
    idle = 0;
}

public void clicked()
{
    total = total + (1 + multiply);
}

public void multiply()
{
    multiply = multiply + 1;
}

public void idle()
{
    idle = idle + 1;
}

public void idleClick()
{
    total = total + idle;
}

public int getTotal()
{
    return total;
}

private int total;
private int multiply;
private int idle;
}

所以这个问题与多次按下“空闲”按钮有关。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您不能多次启动给定的线程。每次按下按钮时,您的代码调用都会在同一个线程对象上启动,从而导致IllegalStateException。您需要重新考虑如何在这里进行多线程处理。

顺便提一下,请注意Swing组件不是线程安全的,不鼓励从事件派发线程以外的线程调用它们。