Java 通过单击按钮运行并暂停GUI后台线程
我需要在Java GUI中运行一个后台线程,该线程仅在我单击按钮时运行,在再次单击该按钮时暂停。我不太确定如何设置它,但我在构造函数中放置了一个线程,其中的while循环设置为在我将特定布尔值设置为TRUE时通过。一个按钮用于切换此布尔值设置为TRUE或FALSE 我在这个GUI中拥有的其他一切都很好。当我试着调试线程时,它实际上在我一步一步地调试线程时起作用,但当我试着完全运行GUI时却什么都没有。GUI相当大,因此我将提供一部分构造函数和按钮的操作侦听器。代码的其余部分是不必要的,因为它工作得很好。我需要知道我做错了什么:Java 通过单击按钮运行并暂停GUI后台线程,java,swing,user-interface,Java,Swing,User Interface,我需要在Java GUI中运行一个后台线程,该线程仅在我单击按钮时运行,在再次单击该按钮时暂停。我不太确定如何设置它,但我在构造函数中放置了一个线程,其中的while循环设置为在我将特定布尔值设置为TRUE时通过。一个按钮用于切换此布尔值设置为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
}
}
ActionListener
建立一个基于Swing的系统actionPerformed(ActionEvent)
方法调用repaint()
timer.Start()
)timer.Stop()
)我以为我有一个“到处都是”。。试试这个,它使用在中创建的图像。我可以看到,当处理按钮事件以影响文本区域或进度条时,这个背景线程对于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();
}
}
}
}
}
我并没有测试这段代码,看看它是否能正常工作,它的主要目标是打破你的编码块,并使用我的组件来解决你的问题。希望这有帮助。还需要什么吗?请发电子邮件给我DesignatedSoftware@gmail.com@LittleChild不,他们不能,被弃用,被认为是不稳定的…@原始海报,请展示一些代码,请告诉我们你到底想做什么,不回答。如果需要更好的帮助,请尽快发布。我不会说@MadProgrammer“没有SSCCE,没有答案”,但这确实暗示了一个事实,即许多人在没有SSCCE的情况下会忽略问题。@AndrewThompson,那只是因为我是一个脾气暴躁、懒惰的老家伙;)你的建议是非常危险的,告诉原始海报进行Swing调用,特别是
jta.append(Integer.toString(i)+“,”)代码>关闭Swing事件分派线程。请修改这个。你的代码可以工作,这正是我希望完成的。看起来我的语法有点不对劲,但如果我能做些调整,我应该能让它正常工作。@PatchGuru你知道我是否能确保文本区域总是在滚动窗格的底部吗?
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);
}
});
}
}