java - 如何使用JProgressBar和java来制作浏览器?

标签 java jcomponent

我正在尝试用 Java 构建一个浏览器。它包含 JProgressBar、JTextField 和 JEditorPane, 但是当我运行程序时,我发现了一个问题:当编辑器 Pane 设置搜索的页面时,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) {

    }

那么编辑器 Pane 是否应该在 JProgressBar 完成时设置页面?

我怎样才能做到这一点?

最佳答案

我认为要达到您想要的行为,您需要做的不仅仅是为编辑器 Pane 设置一个 url 并拥有一个带有进度条等的浏览器。 编写浏览器代码是一项痛苦且复杂的工作,而 JEditorPane 只涵盖了所有可能性中的一小部分。

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

编辑: 使用套接字,您需要执行以下操作(请注意,这是快速且肮脏的,并且没有任何类型的错误处理):

// 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...

关于java - 如何使用JProgressBar和java来制作浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366566/

相关文章:

java - onOptionsItemSelected 未从 fragment 内调用

Java 拆分目标路径与\

java - drawImage() 不绘制

java - paintComponent 和 super.paintComponent

Java - 单击按钮后绘制圆圈

java - 尽管有 Import-Package,但 org.osgi.framework.BundleActivator 的 ClassNotFoundException

java - 我想返回一些东西并执行一些其他方法。 JAVA有什么办法吗?

java - 如何使约束布局的宽度为负数?

java - 如何将 Swing 计时器与 JPanel 一起使用

java - 使用JPanel时JFrame为空