循环期间进度条不更新 [英] Progress bar not updating during a loop

查看:146
本文介绍了循环期间进度条不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的进度栏不会更新,直到循环完成?为什么是这样?

My progress bar doesn't update until the loop has finished? Why is this?

for (String theURL : IPArray) {
    URL url = new URL(theURL);
    InetAddress address = InetAddress.getByName(url.getHost());
    String temp = address.toString();
    String IP = temp.substring(temp.indexOf("/") + 1, temp.length());
    URLArray.add(IP);
    Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo;
    Progress.ipProgress.setString(Progress.percentage + "%");
    Progress.ipProgress.setValue(Progress.percentage);
    Progress.ipProgress.repaint();
    result += IP + System.getProperty("line.separator");
}

它只会在它经过循环后更新,而不是在循环中更新。 / p>

It will only update after it gets past the loop and not during it.

推荐答案

需要新线程。

new Thread(new Runnable() {
    String result = "";

    public void run() {
        for (String theURL : IPArray) {
            try {
                URL url = new URL(theURL);
                InetAddress address = InetAddress.getByName(url.getHost());
                String temp = address.toString();
                String IP = temp.substring(temp.indexOf("/") + 1, temp.length());
                URLArray.add(IP);
                Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo;
                Progress.ipProgress.setString(Progress.percentage + "%");
                Progress.ipProgress.setValue(Progress.percentage);
                Progress.ipProgress.repaint();
                result += IP + System.getProperty("line.separator");
            } catch (Exception e) {
                if ("www.".equals(e.getMessage())) {
                    JOptionPane.showMessageDialog(
                            null, "Incorrect URL. Usage: http://www.URL.com\nError = Space! Can't use gaps in list.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        IPFrame.textAreaIP.setText(result);
        GEOLookup.check(IPFrame.textAreaIP.getText());
    }
}).start();

这篇关于循环期间进度条不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆