gpt4 book ai didi

java - JButton 按下时线程抛出异常

转载 作者:行者123 更新时间:2023-12-02 04:05:15 25 4
gpt4 key购买 nike

所以我有这个程序,它是一种类似“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;
}

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

最佳答案

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

顺便说一句,请注意 Swing 组件不是线程安全的,不鼓励从事件分派(dispatch)线程以外的线程调用它们的方法。

关于java - JButton 按下时线程抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com