1

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);
    }

}

4

1 に答える 1

1

グラフィカルな異常は、イベント ディスパッチ スレッドのブロックが原因である可能性があります。SwingWorker推奨されるアプローチです。ただし、ここに示すように、継続をオブジェクトとして使用することもできます。

于 2010-07-20T01:03:10.760 に答える