您遇到的主要问题是您试图在Event Dispatching
Thread中执行阻止 *** 作。这将防止UI更新,因为直到您完成后,重新绘制请求才到达重新绘制管理器。
为了解决这个问题,您将需要将阻塞工作(即备份过程)卸载到单独的线程中。
为此,我建议您通读Swing
Trail中的并发性,这将为您提供一些解决特定问题的有用策略。特别是,您可能会受益于使用SwingWorker
仔细看看doInBackground及其处理方法
用示例更新
好的,这是一个非常简单的示例。基本上,这会将C:驱动器带到3个目录,然后将内容转储到提供的目录中
Jtextarea
public class BackgroundWorker extends SwingWorker<Object, File> { private Jtextarea textarea; public BackgroundWorker(Jtextarea textarea) { this.textarea = textarea; } @Override protected Object doInBackground() throws Exception { list(new File("C:\"), 0); return null; } @Override protected void process(List<File> chunks) { for (File file : chunks) { textarea.append(file.getPath() + "n"); } textarea.setCaretPosition(textarea.getText().length() - 1); } protected void list(File path, int level) { if (level < 4) { System.out.println(level + " - Listing " + path); File[] files = path.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isFile(); } }); publish(path); for (File file : files) { System.out.println(file); publish(file); } files = path.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !pathname.isHidden(); } }); for (File folder : files) { list(folder, level + 1); } } }}
您只需致电
new BackgroundWorker(textField).execute()并走开:D
用显式示例更新
public class BackgroundWorker extends SwingWorker<Object, String> { private Jtextarea textarea; private File sourceDir; private File destDir; public BackgroundWorker(Jtextarea textarea, File sourceDir, File destDir) { this.textarea = textarea; this.sourceDir = sourceDir; this.destDir = destDirl } @Override protected Object doInBackground() throws Exception { if (sourceDir.isDirectory()) { // if directory not exists, create it if (!destDir.exists()) { destDir.mkdir(); publish("Folder " + sourceDir.getName() + " was created"); } // list all the directory contents String files[] = sourceDir.list(); for (String file : files) { // construct the src and dest file structure File srcFile = new File(sourceDir, file); File destFile = new File(destDir, file); // recursive copy copyFolder(srcFile, destFile); } } else { try { copyFile(sourceDir, destDir); } catch (Exception e) { } } return null; } public void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { // if directory not exists, create it if (!dest.exists()) { publish("Folder " + src.getName() + " was created"); } // 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(src, dest); } catch (Exception e) { } } } public 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(); publish("File copied " + src.getName()); } @Override protected void process(List<String> chunks) { for (String msg : chunks) { textarea.append(msg + "n"); } textarea.setCaretPosition(textarea.getText().length() - 1); }}
现在运行…
new BackgroundWorker(textarea, sourceDir, destDir).execute();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)