这是我正在使用的代码,用于 MAIN 类:
public class Main {
public static void main(String[] args) throws Exception {
MAINFRAME.GUI();
}
}
现在这是关于从主类调用的大型机类:
public class MAINFRAME extends JFrame {
final static JProgressBar PROGBAR = new JProgressBar(0, 100);
final static JButton BUTTON = new JButton("START");
final static JFrame FRM = new JFrame();
private static ZipFile zipFile;
static BufferedInputStream bis;
static java.io.BufferedOutputStream bout;
static java.io.BufferedInputStream in;
public static void GUI() throws Exception {
//Some frame code
frm.add(PROGBAR);
frm.add(BUTTON);
//Some more frame code
BUTTON.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
MAINFRAME.DOWNLOAD();
}
catch (Exception e1) {
e1.printStackTrace();
}
}
});
//Some more code
public static final void DOWNLOAD() throws Exception {
try {
URL url=new URL(URLHERE);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead=0;
in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(FILE);
bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
totalDataRead=totalDataRead+i;
bout.write(data,0,i);
float Percent=(totalDataRead*100)/filesize;
PROGBAR.setValue((int)Percent);
}
bout.close();
in.close();
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
一切正常,下载解压缩等,但应该显示进度的栏一直冻结,直到操作完成,并且在过程完成时继续 100%,现在,我对 InvokeLater 进行了更新,但我真的很难理解如何在这里应用它以及它是如何工作的,提前谢谢你