【问题标题】:Progress Bar Does Not Show Up When Calling a Method调用方法时不显示进度条
【发布时间】:2014-07-08 14:26:39
【问题描述】:

我有一些代码专门下载一个东西,然后有更多代码来下载指定的项目数组,但是由于某种原因,除非我先调用第一个方法然后调用第二个方法,否则进度条不会显示。但是,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[] 位置) {

   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();

}

我不知道我做错了什么,我们将不胜感激!

【问题讨论】:

  • 您正在从另一个线程中调用进度条的setValue。由于主线程和您的其他线程同时运行,您不知道您的线程是否正在完成所有工作,然后主线程可以更新进度条。因此,行为可能难以预测。查看 stackoverflow.com/questions/10342309/… 的线程和 jprogressbar。
  • 编辑:发布了一个错误的 SO 问题的链接:stackoverflow.com/questions/3174613/update-jprogressbar 应该是正确的。
  • 虽然进度条甚至没有显示,但每次在第一种方法中都能正常工作
  • 我认为这只是一个幸运的镜头,它第一次工作。使用 SwingWorker 将使代码以更符合预期的方式运行。

标签: java jframe jprogressbar


【解决方案1】:

您应该使用 SwingWorker。查看 JProgressBar 的 oracle 教程。

http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

【讨论】:

    【解决方案2】:

    您不能从另一个线程更新 UI 组件。所有 UI 操作都应在 EDT(事件调度线程)内完成。您可以尝试使用 SwingUtilities 类执行更新,如下所示:

    SwingUtilities.invokeLater(new Runnable()
    {
       @Override
       public void run()
       {
          progressBar.setValue(percent);
       }
     });
    

    这个runnable将在EDT内部执行

    【讨论】:

    • 不是没有更新,而是界面上根本没有进度条
    猜你喜欢
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多