- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人告诉我,我需要为长期运行的目录树遍历器创建一个 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/
#include #include #include using namespace std::chrono; int main(int arc, char* argv[]) { con
我是 opencv 的新手。当我将 sRGB png 提供给它时,我发现以下代码交换了红色和蓝色 channel 。我应该责怪哪个函数,imread 还是 fromarray? 最佳答案 是的,O
我正在使用以下代码,它缩小了每一行,并且由于某种原因整个用户界面也丢失了。我该如何解决?
它是关于以下设置的:Linux 机器、bash、adb、带有 Busybox 的嵌入式 Linux 目标系统。对于目标系统,以下适用: adb shell echo $SHELL /bin/sh ad
当我在 Android Studio 上创建一个空 fragment 时,它会生成以下代码: /** * A simple {@link Fragment} subclass. * Activit
我正在尝试从 Meteor 应用程序的服务器端发布用户的 Facebook 提要: result = Meteor.http.call 'POST', "https://graph.faceb
目前我有两个不同的查询,它们返回完全相同的结果,但是,更改从中过滤结果的参数会使它们以非常不同的方式运行。 搜索 cartography 时的结果 查询 #1: 22 行/~860 毫秒; SELEC
我已经创建了结构: typedef struct { short s; int i; struct Ss { short s; }; } S;
我想在 Java 中打印反斜杠 t。但每当我尝试时,它实际上都将它作为\t 运算符。双反斜杠不起作用。我该怎么做。 最佳答案 例如通过添加另一个反斜杠来转义反斜杠 System.out.println
我想弄清楚为什么 UIActivityViewController 发送一个稍微转换过的字符串来分享给邮件和微信。 这是我的代码: let activityViewController = UIAct
创建标准 SQLite 游标后,我将使用以下方法遍历条目: while (cursor.moveToNext()) { } 所有行都被正确处理。我读过的所有文档都表明您需要发出 moveToFirst
我正在尝试创建一个基本论坛,但在 SQL 中仅打印一行时遇到问题。这是我的 PHP: {$title}"; } } else { print "failed to reach post
我的新 div 元素 ( ) 似乎隐藏在图像后面。我键入的任何内容都显示在图像后面。我想在背景图片之后继续工作。 这是我的代码: DISPLAY
UPD。一行代码解决了问题:.lean() axplanation here 我在 Model.find(...blablabla : [ {"_id":"578763de6e8e0542195ef4
我在 Ubuntu 16.04 中安装了 Tomcat 8.0.45。我使用 let's encrypt 生成的证书启用了 HTTPS 连接器。它就像一个魅力。但是今天Tomcat无法启动HTTPS连
今天,我在一台全新安装了 Windows 7 Ultimate 64 位的新笔记本电脑上安装了 Visual Studio 2010 Professional。我非常习惯于 Visual Studio
根据 Oracle,我应该能够将 .intValue() 和 .compareTo() 之类的方法应用于 double ,但是当我编写 dbl.toString( ) 例如,在 NetBeans 中,
正在为应用程序开发一些拖放功能,虽然可以使用“重影图像”来完成很多事情,将毒品从一个地方转移到另一个地方,但它们看起来总是有点“褪色” - 因此得名鬼影。是否有可能使这些具有与原始颜色相同的深度?谢谢
我是这方面的新手,很抱歉犯了一些愚蠢的错误。快乐学习。每当我使用 URL : localhost:3000/posts/whatever 时,我都会收到无法获取错误。在你问之前,是的,那是在我将内容放
背景 这自然是合法的: let closure: (Int, Int) -> () = { print($0 + $1) } closure(1, 2) // 3 鉴于,自进化提案实现以来 SE-01
我是一名优秀的程序员,十分优秀!