我正在尝试将百分比附加到 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);
}
}