gpt4 book ai didi

java - 如何修改正在运行的 Java 程序以涉及 SwingWorker(因为输出窗口闪烁并且看起来被黑了)

转载 作者:行者123 更新时间:2023-12-01 23:25:51 24 4
gpt4 key购买 nike

有人告诉我,我需要为长期运行的目录树遍历器创建一个 SwingWorker,其“内容”如下所示。 (为了简单起见,我删除了很多结束 } 和 ) 以及其他多余的(对于我的问题)内容,例如忽略的 catch 子句。)

当匹配给定的文件名模式时,文件名(和其他内容)将通过 report() 写入文本区域:

public class TASK extends SimpleFileVisitor<Path> implements Runnable{
public static FileVisitResult disposition = FileVisitResult.CONTINUE;

private static void report(String s){
try{
Output.jTextArea1.append(s + "\n");
catch (Exception e){ aborted = true ; disposition = FileVisitResult.TERMINATE;}

public void run() {
disposition = FileVisitResult.CONTINUE;
Files.walkFileTree(p , this); // walkTreeFile calls visitFile() for each file.

public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException {
report(f1.getFileName().toString());
return disposition;

main() 只是为 GUI 和输出创建线程:

  public static void main(String args[]) {          // main
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
gui.setVisible(true);

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
info = new Output();

单击“搜索”按钮时,jbSearch...() 使输出窗口可见并实例化 TASK,树遍历开始:

  private void jbSearchActionPerformed(ActionEvent evt) {   // this is in GUI
try {
TASK v = new TASK();
Thread t = new Thread(v);
t.start();

一切正常,但是输出窗口闪烁并且看起来被黑了,所以我需要 SwingWorker。 但我不能说以下内容,因为TASK(或其他东西)必须扩展SimpleFileVisitor:

  class TASK extends SwingWorker<List<String>, String> 

所以我正在寻找要实现的东西。我的笔记告诉我“因为 SwingWorker 实现了 Runnable,所以 SwingWorker 可以提交给 Executor 来执行”,所以我想想除非有人阻止我,否则我接下来就会去那里! (我以前从未使用过执行器。)

在我弄清楚如何涉及 SwingWorker 后,我认为我应该执行以下操作,可能在 jbSearch...() 中代替所示的 3 行(就在上面):

  TASK.execute();

我想我必须让doInBackground()调用visitFile(),这将应用于遍历中的所有文件。 填充 doInBackground() 返回的数组是我的责任吗?SwingWorker 的 Java 教程突然返回 numbers ,而该数字在任何地方都没有定义是一个数组或其他任何东西。

所以我想我必须使process()调用(我的)report(),它将新信息附加到输出窗口(文本区域)。 我假设一旦doInBackground()填充了 block 数组,处理它们就会很容易。

但是我应该把 get() 放在哪里? SwingWorker 的 Java 教程表明 get() 不是必需的,因为该示例写入文本区域,而 get() 仅与 System.out 一起使用以在控制台上显示相同的信息。

(编辑)哦,是的... 我要publish()做什么?我想我在report()内发布。

但我还是要发帖,因为我认为这个问题和/或答案对于提出下一个有关 SwingWorker 问题的人来说可能是一个有值(value)的发现。

(编辑)我认为我面临的一个大问题是 walkFileTree 或多或少"is" 一个循环,重复调用 visitFile() 在幕后,当树结束或当 visitFile() 中的返回值设置为 TERMINATE 时结束。那么我是否只需将 visitFile() 放在 doInBackground() 中,因为它或多或少已经在循环中了?然后将 publish() 放在它后面?

那么我如何返回一个数组呢?

呃。

最佳答案

So I'm looking for something to implement. My notes tell me "Because SwingWorker implements Runnable, a SwingWorker can be submitted to an Executor for execution" so I think I'm heading there next unless somebody stops me! (I've never used executors before.)

After I figure out how in involve SwingWorker, I assume I should do the following, probably in jbSearch...() in place of the 3 lines shown (just above):

TASK.execute();

是的,SwingWorker#execute 是启动 SwingWorker 的典型方法

I guess I have to make doInBackground() call visitFile(), which is what is applied to all the files in the walk. Is it MY responsibility to fill the array that doInBackground() returns? The Java tutorial for SwingWorker suddenly returns numbers which isn't defined anywhere to be an array or anythin else.

您想要在后台完成的任何操作都应该从此方法调用。

是的,您需要自己生成结果。实际值可以在任何地方定义,但我个人在 doInBackground 方法的上下文中定义了一个局部变量

So I guess I have to make process() call (my) report(), which appends new info to the output window (text area). I assume that once doInBackground() fills the array of chunks, it will be easy to process them.

为了处理值,您需要首先发布它们。这些值何时传递给进程将取决于后台进程的密集程度。

重写 done 并调用 get 还可以让您在 EDT 上下文中访问从 doInBackground 返回的内容

But where do I put get()? The Java tutorial for SwingWorker sort of suggests that get() isn't necessary, since the example writes to a text area and get() is merely used with System.out to display the same info on the console. If I don't need to call get(), maybe I'm closer to a solution that I thought when I started this.

这要看情况。你关心结果吗?如果这样做,我倾向于在 done 方法中调用它,因为通常我想使用 EDT 中的值。

关于 get 需要了解的重要一点是它是阻塞的,也就是说,它将等待 doInBackground 返回

对于example

关于java - 如何修改正在运行的 Java 程序以涉及 SwingWorker(因为输出窗口闪烁并且看起来被黑了),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023304/

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