我需要在我的Java GUI中运行一个后台线程,只有当我单击一个按钮时才运行,当我再次单击该按钮时暂停。我不完全确定如何设置这个,但是我已经在构造函数中放置了一个线程,并且当我将特定的布尔值设置为TRUE时,while循环被设置为通过。一个按钮从设置这个布尔值TRUE或FALSE切换。
我在这个GUI中的其他所有内容都可以正常工作。当我尝试调试线程时,它实际上可以在我逐步执行线程时工作,但当我尝试完全运行GUI时却没有任何效果。GUI很大,所以我要放一部分构造函数和按钮的动作监听器。其余的代码是不必要的,因为它工作得很好。我需要知道我在这里做错了什么:
public BasketballGUI() {
// certain labels and buttons
Thread runningSim = new Thread() {
public void run() {
while(simRun) {
// do stuff here
}
}
};
runningSim.start();
}
// other GUI stuff
// actionListener that should run the thread.
class SimButtonListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if(!simRun) {
simRun = true;
sim.setText("Pause Simulator");
}
else if(simRun) {
simRun = false;
sim.setText("Run Simulator");
}
// other stuff in this actionListener
}
}
- 建立一个基于Swing的
Timer
和一个可重复调用的ActionListener
。 - 在
actionPerformed(ActionEvent)
方法中调用repaint()
。 - 当用户点击Start 时启动定时器(
- 当用户点击Stop 时停止定时器(
Timer.start()
)Timer.stop()
)如果你不能从描述中得到它的工作,我建议你发布一个你最好的尝试的SSCCE。
我以为我有一个'躺在'..
我可以看到这个后台线程在Java GUI处理按钮事件以影响文本区域或进度条时非常有用。
为了便于讨论,我将构建一个影响文本区域的小GUI。我希望这对你有帮助。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.*;
public class TestClass extends JPanel {
super("TestClass - Title");
private AtomicBoolean paused;
private JTextArea jta;
private JButton btn;
private Thread thread;
public TestClass() {
paused = new AtomicBoolean(false);
jta = new JTextArea(100, 100);
btn = new JButton();
initialize();
}
public void initialize() {
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
add(new JScrollPane(jta));
btn.setPreferredSize(new Dimension(100, 100));
btn.setText("Pause");
btn.addActionListener(new ButtonListener());
add(btn);
Runnable runnable = new Runnable() {
@Override
public void run() {
while(true) {
for(int i = 0; i < Integer.MAX_VALUE; i++) {
if(paused.get()) {
synchronized(thread) {
try {
thread.wait();
} catch(InterruptedException e) {
}
}
}
}
jta.append(Integer.toString(i) + ", ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
};
thread = new Thread(runnable);
thread.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 30);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
if(!paused.get()) {
btn.setText("Start");
paused.set(true);
} else {
btn.setText("Pause");
paused.set(false);
synchronized(thread) {
thread.notify();
}
}
}
}
}
调用所有内容的主类。
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(final String[] arg) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestClass());
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
}
我没有测试这段代码,看看它是否完全工作,它的主要目标是打破你的编码障碍,并使用我的组件来解决你的问题。希望这对你有所帮助。如有其他需要请发邮件至DesignatedSoftware@gmail.com