我有一些代码专门下载一个东西然后更多的代码来下载指定的项目数组,但是由于某种原因,进度条没有显示,除非我先调用第一个方法然后再调用第二个方法。然而,gui本身确实出现了。这不应该发生,因为代码几乎完全相同,除了一些事情。这是第一个代码:
public void downloadMod(final String url,final String location){
Thread download = new Thread() {
public void run() {
try {
URL website = new URL(url);
HttpURLConnection connection = (HttpURLConnection)website.openConnection();
int size = connection.getContentLength();
float totalDataRead = 0;
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
FileOutputStream fileOutput = new FileOutputStream(location);
BufferedOutputStream output = new BufferedOutputStream(fileOutput, 1024);
byte[] data = new byte[1024];
int i = 0;
while((i = input.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
output.write(data, 0, i);
float percentf = (totalDataRead * 100) /size;
int percent = (int)percentf;
progressBar.setValue(percent);
}
output.close();
input.close();
Zip.unZip(location);
//new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/pack.json").delete();
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/version").delete();
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/minecraft/pack.json").renameTo(new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/pack.json"));
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/minecraft/version").renameTo(new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/version"));
dispose();
new Launch().launch(ModPack.getMod(LauncherGUI.selectedMod));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error Downloading!");
new File(location).delete();
dispose();
}
}
};
download.start();
}
除了多个文件外,第二个几乎完全相同:
public void downloadCrap(final String [] urls,final String [] locations){
Thread download = new Thread() {
public void run() {
for (int j = 0; j < urls.length; j++) {
try {
URL website = new URL(urls[j]);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
int size = connection.getContentLength();
float totalDataRead = 0;
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
FileOutputStream fileOutput = new FileOutputStream(locations[j]);
BufferedOutputStream output = new BufferedOutputStream(fileOutput, 1024);
byte[] data = new byte[1024];
int i = 0;
while ((i = input.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
output.write(data, 0, i);
float percentf = (totalDataRead * 100) / size;
int percent = (int) percentf;
progressBar.setValue(percent);
}
output.close();
input.close();
}
catch (Exception e) {
new File(locations[j]).delete();
}
}
}
};
download.start();
try {
download.join();
}
catch(Exception e) {
e.printStackTrace();
}
dispose();
}
我不知道我做错了什么,感谢帮助!
答案 0 :(得分:0)
您应该使用SwingWorker。查看JProgressBar的oracle教程。
http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html
答案 1 :(得分:0)
您无法从其他线程更新UI组件。所有UI操作都应该在EDT(事件调度程序线程)内完成。您可以尝试使用SwingUtilities类执行更新,如下所示:
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progressBar.setValue(percent);
}
});
此runnable将在EDT内部执行