当前位置: 首页 > 面试题库 >

JTextArea附加问题

谯皓君
2023-03-14
问题内容

我正在制作一个备份程序,我希望将要备份的程序显示在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);
}
    }

问题答案:

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


 类似资料:
  • 问题内容: 我在JTabbedPane中的(多个)JScrollPane中有一个JTextArea。 我需要访问JTextArea。如果没有JScrollPane,则可以执行以下操作: 在JScrollPane中如何获取? 干杯,瞪羚。 问题答案: 这条线看起来很复杂,但是我 认为 可以做到这一点。 但是我认为将您的存储在一个。 因此,您可以执行以下操作: 创建一个新的是这样的: 希望这可以帮助。

  • 问题内容: 我正在为我的大学课程设计一个项目。我只是想知道是否有人知道如何向JTextArea添加scrollBar。目前,我的GUI 布局正确,唯一缺少的是滚动条。 这就是GUI的外观。如您在第二个TextArea上看到的,我想添加滚动条。 这是我创建窗格的代码。但是似乎什么也没发生……t2是 我要添加到的JTextArea。 任何帮助将是巨大的,谢谢! 问题答案: 当您的文本超出查看区域的范围

  • 问题内容: 我正在尝试创建一个每次将文本添加到该文本区域时都会滚动到底部的。否则,用户应该能够向上滚动并查看上一条消息。我使用以下代码: 到目前为止,这段代码似乎使我每次向using 附加内容时,文本区域都滚动到文本区域的底部。 但是,用户不能使用滚动条滚动到顶部以查看上一条消息。有没有办法解决这个问题?我应该用来实现这一目标吗? 问题答案: 签出智能滚动。 如果滚动条在底部,则在添加文本后,您将

  • 我有以下值给出了错误的总数。 当我用计算器计算上述值时,它给出了我预期的结果,而javascript给出了我意想不到的结果。为什么和什么是解决方案才能得到预期的结果?

  • 介绍 (Introduction) JTextArea类是一个显示纯文本的多行区域。 Class 声明 (Class Declaration) 以下是javax.swing.JTextArea类的声明 - public class JTextArea extends JTextComponent 类构造函数 (Class Constructors) Sr.No. 构造函数和描述 1 JTe

  • 如果有人能帮我摆脱困境,请告诉我。多谢了。