如何使用java的JProgressBar制作浏览器?

时间:2014-12-08 20:45:00

标签: java jcomponent

我试图用Java构建浏览器。它包含JProgressBar,JTextField和JEditorPane, 但是当我运行程序时,我发现了一个问题:当编辑器窗格设置搜索的页面时,JProgressBar不起作用。

我已尝试过此代码:

  String az = jTextField1.getText();


       if(az.contains("1")){
            String hh = WorkSpace.jTextField1.getText();


    try {
         WorkSpace.jEditorPane1.setPage("" + hh );
         WorkSpace.jProgressBar1.setValue(); // which value?

    } catch (Exception e) {

    }

编辑窗格应该在JProgressBar完成时设置页面吗?

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我认为要达到您想要的行为,您需要做的不仅仅是在您的编辑器窗格中设置一个网址,并使用带有进度条等的浏览器。 编写brwoser是一项痛苦而复杂的工作,而JEditorPane只涵盖了所有可能性。

但是,为了满足您的需求,我认为您需要获取您自己尝试显示的页面内容(使用套接字或httpclient或任何其他库)。从服务器接收字节时,您可以更新进度条。收到所有字节后,您将内容一步设置到应显示内容的窗格。

修改 使用套接字,您需要执行以下操作(请注意,这是快速和脏的,没有任何错误处理):

// Suppose you want to display http://www.target.com/page
Socket s = new Socket("target.com", 80);
PrintWriter out = new PrintWriter(s.getOutputStream());
// Tell the server you want to get "/page"
out.println("GET /page HTTP/1.1");
out.println("Host: target.com");
out.println();

// The target-Server now send you the content of "/page"
// Now you need to know a little about the HTTP-Protocol.
// In short: The server sends you a header and a body. 
// The header and the body is separated using two newlines.
// You need to read line by line from the server until the 
// body starts and interpret the stuff from the header because it
// contains the information how many bytes you will receive with the body
// ( -> Content-Length: xyz)
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

// Read the Header and interpret that stuff.
doReadHeader(in);

// Now, because of the Content-Length Header you know how many bytes you need to read
// from the InputStream until all the bytes are receive. Thus you can update your
// progressbar while receiving the bytes
doReadBody(in);

这就是它。收到所有这些后,您可以将主体1:1设置到EditorPane中。但要注意,EditorPane只包含很少的HTML和CSS。所以也许你需要使用其他HTML-Pane,如FlyingSaucer或CSSBox ......