gpt4 book ai didi

java - 在执行其他方法时在单独的线程上更新 GUI

转载 作者:行者123 更新时间:2023-11-30 06:22:11 25 4
gpt4 key购买 nike

我开发了一个 GUI,它有几个按钮和一个用于输出的 JTextArea。我下面有一些执行函数并更新 TextArea 的代码,但是在执行该函数之前,我希望使用字符串“Processing...”更新 TextArea,只是为了让用户知道该应用程序是由于该函数需要一段时间才能执行。

目前此代码不会更新 GUI 元素,我明白原因。 GUI 没有机会在更改 TextArea 的两个命令之间重新绘制,因此永远不会显示“processing...”字符串。如何更改代码以便 GUI 元素在 Main.featureAnalysisTop() 函数执行之前更新?

if(e.getActionCommand().equals("Extract Features"))
{
featuresTextArea.setText("Processing...");
int nFeatures = nFeatureSlider.getValue();
Main.featureAnalysisTop(nFeatures);

featuresTextArea.setText("");
ArrayList<String> featureList = Main.getFeatureList();

for(String str : featureList)
{
featuresTextArea.append(str + "\n");
}

使用以下代码在我的主方法中执行 GUI。

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gui = new GUI2();
gui.setLocationRelativeTo(null);
gui.frmNeuralNetwork.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

最佳答案

Updating GUI on separate thread while other method is executing

这是 Swing Woker 的工作类,它允许您创建一个在后台运行的单独线程,并让您相应地更新 GUI。

例如,我做了一个简单的示例来完成您想要做的事情。

首先我们创建 GUI 并添加 ActionListener到我们的JButton它从哪里开始我们的 Swing Worker更新后JTextArea的短信至processing...当您尝试执行此操作时,5 秒后(模拟您的长时间运行任务)它会更新为 I'm done! .

您可以尝试一下并相应地更改代码。

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class TextAreaUpdater {
private JFrame frame;
private JTextArea area;
private JButton button;
private SwingWorker<String, String> worker;

public static void main(String[] args) {
SwingUtilities.invokeLater(new TextAreaUpdater()::createAndShowGui);
}

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

area = new JTextArea(10, 30);

button = new JButton("Update!");
button.addActionListener(listener);

worker = new SwingWorker<String, String>() {
@Override
protected String doInBackground() throws Exception {
try {
Thread.sleep(5000); //Simulates long running task
} catch (InterruptedException e) {
e.printStackTrace();
}
return "I'm done!"; //Returns the text to be set on the JTextArea
}

@Override
protected void done() {
super.done();
try {
area.setText(get()); //Set the textArea the text given from the long running task
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
};

frame.add(area, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private ActionListener listener = (e -> {
area.setText("Processing...");
worker.execute(); //Initializes long running task
});
}

引用文献:

这是另一个使用 SwingWorker 的示例: update jlabel text after opening jdialog

希望对你有帮助

关于java - 在执行其他方法时在单独的线程上更新 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891172/

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