栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JTextArea附加问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JTextArea附加问题

您遇到的主要问题是您试图在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();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号