我正在製作一個備份程序,而且我希望所有的程序備份都顯示在JTextArea上。好吧,它的工作,但只有在程序完成備份後。我該如何解決?我已經運行該代碼是在這裏:JTextArea附加問題
備份方法
public void startBackup() throws Exception {
// txtarea is the JTextArea
Panel.txtArea.append("Starting Backup...\n");
for (int i = 0; i < al.size(); i++) {
//al is an ArrayList that holds all of the backup assignments selected
// from the JFileChooser
File file = new File((String) al.get(i));
File directory = new File(dir);
CopyFolder.copyFolder(file, directory);
}
}
複製文件夾類:
public class CopyFolder {
public static void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
// if directory not exists, create it
if (!dest.exists()) {
dest.mkdir();
Panel.txtArea.append("Folder " + src.getName()
+ " was created\n");
}
// list all the directory contents
String files[] = src.list();
for (String file : files) {
// construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// recursive copy
copyFolder(srcFile, destFile);
}
} else {
try {
CopyFile.copyFile(src, dest);
} catch (Exception e) {
}
}
}
}
的CopyFile類
public class CopyFile {
public static void copyFile(File src, File dest) throws Exception {
// if file, then copy it
// Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
// System.out.println("File copied from " + src + " to " + dest);
Panel.txtArea.append("File copied " + src.getName() + "\n");
}
}
感謝提前的幫助下,讓我知道我可以給予的任何幫助。我做了一個谷歌搜索,這似乎是一個大問題,但我不能想到如何解決它。噢,請不要因爲它不適用於你而使其失望,因爲它非常讓人加重。再次感謝您!
編輯: 這就是我得到:
public class test extends SwingWorker<Void, String> {
String txt;
JTextArea txtArea = null;
public test(JTextArea txtArea, String str) {
txt = str;
this.txtArea = txtArea;
}
protected Void doInBackground() throws Exception {
return null;
}
protected void process(String str) {
txtArea.append(str);
}
protected void getString() {
publish(txt);
}
}
您試圖從SwingWorker的'doInBackground()'方法內進行Swing調用,這與您想要做的完全相反。請閱讀教程以瞭解如何使用此工具。 – 2012-08-13 02:50:01
哦對不起,我忘記刪除,當我重新編輯。對不起 – PulsePanda 2012-08-13 02:52:08
它可能是一個良好的夜間睡眠將有所幫助。現在去睡覺。所以只是我不回答。 – PulsePanda 2012-08-13 03:01:04