当尝试附加一行文本时,窗口显示为“透视” - Java

时间:2010-07-19 16:24:24

标签: java monitoring has-many-through progress

我试图将一个百分比附加到java中的文本区域。它涉及一个确定百分比的循环,然后将其附加到另一个带有文本区域的JFrame。

“pro”类只有一个带有JtextArea的窗口。

我遇到的问题是窗口似乎显示下面的窗口,好像它是滞后的。无论如何解决这个问题。我试过看SwingWorker,但我发现它令人困惑。任何帮助将不胜感激。以下是该计划的摘录。

public void copy(File sourceLocation, File targetLocation) throws IOException {

  if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i=0; i<children.length; i++) {

            int length = children.length - 1;

            float percentage = (i/(float)length) *100;
            String d = percentage + "%" + " " + sourceLocation;
            System.out.println(percentage + "%" + " " + sourceLocation);

            pro.area.append(percentage + "\n");




            copy(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        //pro.setVisible(false);
    }

}

1 个答案:

答案 0 :(得分:1)

图形异常可能是由于阻止了event dispatch threadSwingWorker是首选方法;但使用continuations as objects是另一种选择,如here所示。