gpt4 book ai didi

java - 在 JTextArea 中显示后台进程状态

转载 作者:行者123 更新时间:2023-11-29 07:07:39 26 4
gpt4 key购买 nike

有什么方法可以在JTextArea 中写入有关后台进程的文本。我在屏幕上只有一个按钮。当用户单击该按钮时,将启动一些进程。我想在文本区域的屏幕上显示该进程的持续状态。在完成 textArea.append("Some status") 之后,我会调用 textArea.repaint() 但这对我不起作用。

我是否需要为此实现自己的线程?

最佳答案

您只需使用 SwingWorker ,您真的不必担心在 Event Dispatcher Thread 上执行周期性任务。您只需在 doInBackground() 方法中调用 publish() 将指定的文本附加到 JTextArea,这将调用 process() 并在 Event Dispatcher Thread 上自动执行整个任务。

看看这个工作示例:

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class LoadingImage {

private JPanel contentPane;
private JTextArea logArea;
private JLabel imageLabel;
private ImageIcon[] images;
private JButton startStopButton;
private String[] path;
private int counter;

private Timer timer;

private ActionListener timerAction =
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
counter %= path.length;
imageLabel.setIcon(images[counter++]);
}
};

private ActionListener buttonAction =
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (timer.isRunning()) {
startStopButton.setText("Start");
timer.stop();
}
else {
startStopButton.setText("Stop");
timer.start();
}
}
};

public LoadingImage() {
imageLabel = new JLabel("Nothing to display yet...", JLabel.CENTER);
images = new ImageIcon[5];
path = new String[] {
"http://i.imgur.com/922oehL.gif",
"http://i.imgur.com/2Fim5t4.gif",
"http://i.imgur.com/jJKlCiI.gif",
"http://i.imgur.com/0KuZuGl.gif",
"http://i.imgur.com/evuKoI5.gif"
};
counter = 0;
}

private void displayGUI() {
JFrame frame = new JFrame("Loading Image Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

contentPane = new JPanel(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel(new GridLayout(1, 2, 5, 5));
logArea = new JTextArea(10, 10);
JScrollPane logScroller = new JScrollPane();
logScroller.setViewportView(logArea);

centerPanel.add(logScroller);
centerPanel.add(imageLabel);
contentPane.add(centerPanel, BorderLayout.CENTER);

startStopButton = new JButton("Stop");
startStopButton.addActionListener(buttonAction);
contentPane.add(startStopButton, BorderLayout.PAGE_END);

frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

new BackgroundTask().execute();
timer = new Timer(1000, timerAction);
timer.start();
}

private class BackgroundTask extends SwingWorker<ImageIcon[], String> {
@Override
protected ImageIcon[] doInBackground() {
ImageIcon[] images = new ImageIcon[path.length];
for (int i = 0; i < path.length; i++)
{
try {
images[i] = new ImageIcon(ImageIO.read(new URL(path[i])));
}catch(Exception e) {e.printStackTrace();}
publish(String.format("Loaded : %s%n", path[i]));
}

return images;
}

@Override
protected void process(java.util.List<String> messages) {
for (String message : messages)
logArea.append(message);
}

@Override
protected void done() {
try {
images = get();
} catch(Exception e) {e.printStackTrace();}
}
}

public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new LoadingImage().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

关于java - 在 JTextArea 中显示后台进程状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18031956/

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