因此,我有了这个程序,该程序是诸如游戏之类的“ Cookie Clicker”的基础,而且我设法弄清楚了如何使其保持玩家已完成的总点击次数以及空闲的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;
}


因此,问题与多次按下“空闲”按钮有关。任何帮助将非常感激。

最佳答案

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

顺便说一句,请注意,Swing组件不是线程安全的,因此不建议从事件分发线程之外的其他线程调用它们。

10-07 19:09
查看更多