简体   繁体   English

如何在Java中使用JProgressBar创建浏览器?

[英]How to use a JProgressBar with java to make a browser?

I'm trying to build a browser in Java. 我正在尝试用Java构建浏览器。 It contains JProgressBar, JTextField and JEditorPane, but when I ran the program I found a problem: When the editor pane set the page searched, the JProgressBar doesn't work. 它包含JProgressBar,JTextField和JEditorPane,但是当我运行该程序时,我发现了一个问题:当编辑器窗格设置了要搜索的页面时,JProgressBar不起作用。

I've tried this code: 我已经试过这段代码:

  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) {

    }

So should the editor pane set the page when the JProgressBar is complete? 那么,当JProgressBar完成时,编辑器窗格是否应该设置页面?

How I can do this? 我该怎么做?

I think to reach your desired behavior you need to do a little bit more than just set a url to your editor pane and have a browser with progress bars etc.. Coding a brwoser is a painful and complicate work and JEditorPane only covers a little of all possibilities. 我认为要达到您想要的行为,您需要做的不只是将URL设置到编辑器窗格中,而要使用带有进度条的浏览器等等。编码浏览器是一项痛苦而复杂的工作,而JEditorPane仅涵盖了一些内容。所有的可能性。

However, to cover your needs I think you need to get the content of the page you try to display by your own (using Sockets or httpclient or any other lib). 但是,为了满足您的需求,我认为您需要获取尝试自行显示的页面的内容(使用Sockets或httpclient或任何其他lib)。 While you receive the bytes from the server you can update the progressbar. 当您从服务器收到字节时,可以更新进度条。 After you've received all the bytes you set the content in one step to the pane which should display the content. 收到所有字节后,只需一步即可将内容设置到应显示内容的窗格中。

Edit: Using Sockets you need to do the following (note that this is quick and dirty an without any kind of error handling): 编辑:使用套接字,您需要执行以下操作(请注意,这是快速且肮脏的,没有任何类型的错误处理):

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

That pretty much is it. 差不多了。 After you received all that, you can set the body 1:1 into your EditorPane. 收到所有内容后,可以将主体1:1设置到EditorPane中。 But beware the EditorPane just covers little of HTML and CSS. 但是请注意,EditorPane仅涵盖了很少的HTML和CSS。 So maybe you need to go with another HTML-Pane like FlyingSaucer or CSSBox... 因此,也许您需要使用其他HTML窗格,例如FlyingSaucer或CSSBox ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM