현재 파일을 다운로드하는 동안 간단한 양식을 보여줄 클래스가 있습니다. 작동하지만 진행률 표시 줄이 업데이트되지 않고 다운로드가 완료되면 볼 수 있습니다. 누군가 나를 도울 수 있습니까?스윙 GUI를 사용하여 파일을 다운로드하는 동안 진도 표시 줄을 표시하십시오.
import java.awt.FlowLayout;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JProgressBar;
public class Downloader {
public Downloader(String site, File file) {
JFrame frm = new JFrame();
JProgressBar current = new JProgressBar(0, 100);
current.setSize(50, 100);
current.setValue(0);
current.setStringPainted(true);
frm.add(current);
frm.setVisible(true);
frm.setLayout(new FlowLayout());
frm.setSize(50, 100);
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
URL url = new URL(site);
HttpURLConnection connection
= (HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead = 0;
try (java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream())) {
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
try (java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024)) {
byte[] data = new byte[1024];
int i;
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
float Percent = (totalDataRead * 100)/filesize;
current.setValue((int) Percent);
}
}
}
} catch (IOException e) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, e.getMessage(), "Error",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
}
은 EDT (이벤트 발송 쓰레드)를 막지 마십시오. 대신 오랫동안 실행되는 작업을 위해'SwingWorker'를 구현하십시오. 자세한 내용은 [동시성의 동시성] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)을 참조하십시오. –
그리고 또 다른 dup : [JProgressBar가 다운로드하는 동안 업데이트되지 않습니다.] (http://stackoverflow.com/questions/17400982/jprogressbar-doesnt-update-during-download) –
아직 세 번째 : [내 JProgressBar가 업데이트되지 않습니다. 때까지 100 %] (http://stackoverflow.com/questions/10437590/my-jprogressbar-is-not-updating-until-it-is-100). Google 기술에 대한 작업 시간! ;) –