我有一个类,extends JFrame
在里面我有一个方法如下:
public void downloadUrl(String filename, String urlString) throws MalformedURLException, IOException
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
int modPackSize = getModPackSize();
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
downloadedPerc += (count*1.0/modPackSize)*100;
progressBar.setValue((int) downloadedPerc);
label.setText((int) downloadedPerc + "%");
System.out.println(downloadedPerc);
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (in != null)
in.close();
if (fout != null)
fout.flush();
fout.close();
}
}
此方法下载文件并获取下载百分比。当它运行时,我的 JFrame 是空白的。运行后,JFrame 会更新并正确显示,但我希望它经常更新(嗯,首先显示自己),我该怎么做?