Java 当我开始这个动作[下载]时,为什么我的帧会冻结

Java 当我开始这个动作[下载]时,为什么我的帧会冻结,java,jframe,actionlistener,Java,Jframe,Actionlistener,我制作了一个带有“开始下载”按钮的框架,用于从网站下载一个JAR 问题是每当我点击开始下载按钮时,整个画面都会冻结,直到下载完成,然后才恢复正常 我该怎么修 下面是单击按钮时执行的代码 private void addToDesktop() throws IOException { URL url = new URL("urlremoved"); URLConnection connection = url.openConnection(); InputStream

我制作了一个带有“开始下载”按钮的框架,用于从网站下载一个JAR

问题是每当我点击开始下载按钮时,整个画面都会冻结,直到下载完成,然后才恢复正常

我该怎么修

下面是单击按钮时执行的代码

 private void addToDesktop() throws IOException {

    URL url = new URL("urlremoved");
    URLConnection connection = url.openConnection();
    InputStream inputstream = connection.getInputStream();
    FileSystemView filesys = FileSystemView.getFileSystemView();
    filesys.getHomeDirectory();
    sizeOfClient = connection.getContentLength();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(new File(clientDL.textField1.getText() + "/jarname.jar")));
    byte[] buffer = new byte[2048];
    int length;


    while(( length = inputstream.read(buffer)) > -1)
    {
        down += length;
        bufferedoutputstream.write(buffer, 0 , length);
         String text = clientDL.label1.getText();
        int perc = getPerc();

        if(perc <= 50)
        {
            text +=  getPerc() + "% done";
        }else
        {
            text ="Please wait until the jar is downloading...";
            text = text + (100 - perc) + " % remaining";
        }
    }

    if (down == sizeOfClient) {
        JOptionPane.showMessageDialog(clientDL.frame, "Download successful. It has been placed at : " + clientDL.textField1.getText() + "/jarname.jar", "Success!", JOptionPane.INFORMATION_MESSAGE);
        clientDL.frame.dispose();
        clientDL.frame.setVisible(false);

    }
    bufferedoutputstream.flush();
    bufferedoutputstream.close();
    inputstream.close();
    hideSplashScreen();
}
private void addToDesktop()引发IOException{
URL=新URL(“URL已删除”);
URLConnection=url.openConnection();
InputStream InputStream=connection.getInputStream();
FileSystemView filesys=FileSystemView.getFileSystemView();
filesys.getHomeDirectory();
sizeOfClient=connection.getContentLength();
BufferedOutputStream BufferedOutputStream=new BufferedOutputStream(新文件(clientDL.textField1.getText()+“/jarname.jar”));
字节[]缓冲区=新字节[2048];
整数长度;
而((长度=inputstream.read(缓冲区))>-1)
{
向下+=长度;
写(缓冲区,0,长度);
String text=clientDL.label1.getText();
int perc=getPerc();

if(perc简短回答:如果不希望它冻结,则需要在单独的线程上运行它

实现这一点的方法有很多种。几乎所有方法都需要将addToDestop()方法提取到可运行类中。此类可以扩展Thread或SwingWorker或任何类似的性质

您可以检查SwingWorker的以下链接

下面的伪代码会给你一个想法

public class Downloader extends SwingWorker<VOID, VOID> {
    private String url;

    public Downloader(String url){
        this.url = url;
    }

    private void addToDesktop(){
        //your code
    }

    @override
    protected void doInBackground(){
        addToDesktop();
    }

    @override
    protected void done(){
        //success
    }
}
公共类下载程序扩展SwingWorker{
私有字符串url;
公共下载程序(字符串url){
this.url=url;
}
私有void addToDesktop(){
//你的代码
}
@凌驾
受保护的void doInBackground(){
addToDesktop();
}
@凌驾
受保护的void done(){
//成功
}
}

尝试使用线程,它将停止冻结您的帧

private Thread thread;


你好,harun,谢谢你的简短回复,我该怎么做呢?我还是新的:c谢谢!谢谢!工作得很好:)我只需要捕获IOException,而不是抛出它。-1.不要在EDT之外更新你的GUI。而且你没有放解释器。
thread = new Thread(new Runnable(){
public void run(){
//Your code here
}
});
thread.start();