java catch进程时间实时进度条

时间:2017-04-07 12:56:32

标签: java swing time process

new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();

使用这个^我从.txt文件中获取数据,我将其保存在字符串中。

对于我的progress bar,我想知道这样的工作(或者通常用于方法等)是否有可能计算(或者像这样)需要完成的时间。这样我就可以在process time中实时显示bar

这有可能吗?

enter image description here

修改

package app.gui;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.net.URL;
    import java.util.Scanner;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;

    import org.apache.commons.io.IOUtils;

    public class Updater {

        private JFrame frame;
        private static String rawG;
        private static String versI;

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        rawG = new Scanner(new URL("SOME_URL.txt").openStream(), "UTF-8").useDelimiter("\\A").next();
                        versI = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("version.txt"));
                    } catch (Exception e) {
                        System.out.println("error class Updater try/catch raw github");
                    }
                    if (Integer.parseInt(rawG.split("\\.")[0]) < Integer.parseInt(versI.split("\\.")[0])) {
                        System.out.println("Version check failure, update needed");
                        try {
                            Updater window = new Updater();
                            window.frame.setVisible(true);
                        } catch (Exception e) {
                            System.out.println("error class Updater try/catch initialize frame");
                        }                   
                    } else {
                        System.out.println("Version check correct, no update needed");
                    }
                }
            });
        }

        public Updater() {
            initialize();
        }

        private void initialize() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            frame.getContentPane().add(panel, BorderLayout.SOUTH);
            panel.setLayout(new BorderLayout(0, 0));

            JProgressBar progressBar = new JProgressBar();
            progressBar.setStringPainted(true);
            panel.add(progressBar, BorderLayout.NORTH);
        }

    }

1 个答案:

答案 0 :(得分:3)

有可能吗?是。使用Scanner.next()读取URL的内容是否可行?否。

您需要自己读取字节并计算它们:

URL url = new URL("SOME_URL.txt");
URLConnection conn = url.openConnection();

ByteBuffer buffer = ByteBuffer.allocate(conn.getContentLength());
EventQueue.invokeLater(() -> progressBar.setMaximum(buffer.limit()));
EventQueue.invokeLater(() -> progressBar.setValue(0));
try (ReadableByteChannel channel = Channels.newChannel(conn.getInputStream())) {
    while (channel.read(buffer) >= 0) {
        EventQueue.invokeLater(() -> progressBar.setValue(buffer.position()));
    }
}

buffer.flip();
String rawG = StandardCharsets.UTF_8.decode(buffer).toString();