如何在使用java从Socket接收字节后更新JProgressBar?

时间:2014-12-09 16:36:15

标签: java netbeans

我尝试使用带有JProgressBar的java构建浏览器几天。 所以我可以像这样从Socket获取字节:

    try {

         Socket s = new Socket("target.com", 80);
         PrintWriter out = new PrintWriter(s.getOutputStream());
         out.println("GET /page HTTP/1.1");
         out.println("Host: target.com");
         out.println();
         BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));


    } catch (Exception e) {

    }

如何更新JProgressBar以设置JEditorPane中的网页?

1 个答案:

答案 0 :(得分:0)

同时这是一个非常简单的工作示例。所有积分均转至zentut

它应该让你开始使用基本的JProgressBar设置。

package jprogressbardemo;

import java.awt.*;
import javax.swing.*;

public class Demo {

    public static void main(String[] args) {
        final int MAX = 100;
        final JFrame frame = new JFrame("JProgress Demo");

        // creates progress bar
        final JProgressBar pb = new JProgressBar();


        pb.setMinimum(0);
        pb.setMaximum(MAX);
        pb.setStringPainted(true);

        // add progress bar
        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(pb);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

        // update progressbar
        for (int i = 0; i <= MAX; i++) {
            final int currentValue = i;
            try {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        pb.setValue(currentValue);
                    }
                });
                java.lang.Thread.sleep(100);
            } catch (InterruptedException e) {
                JOptionPane.showMessageDialog(frame, e.getMessage());
            }
        }

    }
}