我试图让这个名为 textArea
的 JTextArea
在复制这些照片时进行更新,但我似乎无法让它正常工作。我正在使用这段代码:
String name = "";
int numberOfPicturesCopied = 0;
while (pictures.isEmpty() == f) {
try {
File tmp = pictures.firstElement();
name = tmp.getName();
String filename = destination + Meta.date(tmp) + tmp.getName();
Path source = tmp.toPath();
File destFile = new File(filename);
Path destination = destFile.toPath();
Files.copy(source, destination,
StandardCopyOption.COPY_ATTRIBUTES);
textArea.append("Copied " + name + "\n");
pictures.removeElementAt(0);
numberOfPicturesCopied++;
} catch (FileAlreadyExistsException faee) {
textArea.append("Skipped " + name
+ ": Picture Already In Computer\n");
} catch (NoSuchFileException ncfe) {
File tmp = pictures.firstElement();
String filename = destination + Meta.date(tmp);
File newDir = new File(filename);
newDir.mkdir();
} catch (IOException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}
}
然后我将其更改为:
public void copyPictures(){
SwingUtilities.invokeLater(new Thread(){
public void run(){
String name = "";
while(pictures.isEmpty() == f){
try {
File tmp = pictures.firstElement();
name = tmp.getName();
String filename = destination + Meta.date(tmp) + tmp.getName();
Path source = tmp.toPath();
File destFile = new File(filename);
Path destination = destFile.toPath();
Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES);
textArea.append("Copied " + name + "\n");
pictures.removeElementAt(0);
numberOfPicturesCopied++;
} catch(FileAlreadyExistsException faee){
textArea.append("Skipped " + name +": Picture Already In Computer\n");
} catch (NoSuchFileException ncfe){
File tmp = pictures.firstElement();
String filename = destination + Meta.date(tmp);
File newDir = new File(filename);
newDir.mkdir();
} catch (IOException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}
}
}
});
}
结果相同。有什么建议吗?
另外,有什么方法可以让文本出现在文本区域的顶部吗?
最佳答案
如何在开头插入文本已经得到解答。您问题的另一部分与往常一样...您正在事件调度线程上执行繁重的工作,该线程不再能够执行重绘。
您应该做的是在工作线程上执行繁重的工作,并且仅在 EDT 上更新 UI。例如,您可以使用专门为此设计的 SwingWorker
。或者更简单,采用您当前的代码并进行一些简单的修改
public void copyPictures(){
new Thread(){
public void run(){
while(pictures.isEmpty() == f){
try {
File tmp = pictures.firstElement();
final String name = tmp.getName();
String filename = destination + Meta.date(tmp) + tmp.getName();
Path source = tmp.toPath();
File destFile = new File(filename);
Path destination = destFile.toPath();
Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES);
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
textArea.append("Copied " + name + "\n");
}
}
);
pictures.removeElementAt(0);
numberOfPicturesCopied++;
} catch(FileAlreadyExistsException faee){
textArea.append("Skipped " + name +": Picture Already In Computer\n");
} catch (NoSuchFileException ncfe){
File tmp = pictures.firstElement();
String filename = destination + Meta.date(tmp);
File newDir = new File(filename);
newDir.mkdir();
} catch (IOException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}
}
}
}.run();
}
查看工作是如何在单独的线程
上完成的,但 UI 是如何在 EDT 上更新的。更多信息可参见Swing Concurrency tutorial或 SO(您的搜索关键字是 SwingWorker
,这将导致一堆示例,因为这是一个日常问题)
关于java - 需要在JTextArea中实时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474963/