执行其他行后正在运行方法

问题描述

我有一个通过单击按钮下载文件的程序,我想在进度条上显示下载百分比,但进度条没有更新,我使用了一个 void 类使其不在 doInBackground 方法一个摇摆工人,但是当我运行代码时,它首先下载文件,下载后显示一条消息,而不是在下载方法旁边运行进度条更新方法,所以我搜索了谷歌,发现我可以使用线程,但是当我使用线程 它不执行线程中的任何方法。 这是我的线程代码

package mcsm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import javax.swing.SwingWorker;
import static mcsm.MCSM1.jLabel2;
import static mcsm.MCSM1.jProgressBar1;
public class download {
    static SwingWorker sw1 = new SwingWorker() {
        @Override
        protected String doInBackground() throws Exception {
            String fileName = "Hi.jar";
            File spigotjar = new File(fileName);
            
            if (!(spigotjar.exists())) {
                Thread dlth = new Thread() {
                    public void rundl() throws Exception {
                        System.out.println("Downloading !?!");
                        URL url = new URL("Some URL");
                        try (InputStream in = url.openStream(); ReadableByteChannel rbc = Channels.newChannel( in ); FileOutputStream fos = new FileOutputStream("Hi.jar")) {
                            fos.getChannel().transferFrom(rbc,Long.MAX_VALUE);
                            URLConnection urlConnection = url.openConnection();
                            urlConnection.connect();
                            System.out.println("Wait,thats illegal.");
                        }
                    }
                };
                Thread dlpr = new Thread() {
                    public void prdl() throws Exception {
                        dlprogress();
                    }
                };
                dlth.start();
                dlpr.start();
            }
            if (spigotjar.exists()) {
                Fileexists();
                System.out.println("hey ?!");
            }
            String res = "Finished Execution";
            return res;
        }
    };
    public static void Fileexists() {
        jLabel2.setText("The file exists already !");
    }
    public static void dlprogress() throws Exception {
        String fileName = "Hi.jar";
        File spigotjar = new File(fileName);
        URL url = new URL("Some more URL");
        jLabel2.setText("Downloading the file from Bukkit website....");
        URLConnection urlConnection = url.openConnection();
        int dl_size = urlConnection.getContentLength();
        int dlsize_kb = dl_size / 1024;
        int dlsize_mb = dlsize_kb / 1024;
        long file_sizel = spigotjar.length();
        int file_size = (int) file_sizel;
        float file_size_kb = file_size / 1024;
        float file_size_mb = file_size_kb / 1024;
        float total_per_multi = 100 / dlsize_mb;
        float percentage = file_size_mb * total_per_multi;
        while (file_size != dl_size) {
            jProgressBar1.setValue((int) percentage);
            System.out.println("Working Fine....");
        }
        while (file_size == dl_size) {
            jProgressBar1.setValue(0);
            jLabel2.setText("Download Finished!");
        }
    }
}

抱歉,如果不清楚,我的主要语言不是英语。

解决方法

Thread dlpr = new Thread() {
      // changed prdl() --> run()
      public void run() throws Exception {
             dlprogress();
      }
};

您创建的这个匿名内部类扩展了 Thread 类,需要重写 run() 方法。因为在调用 dlpr.start() 内部会调用 run() 方法。 您能否尝试在两个线程中将方法名称更改为 run()。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...