JPanel内置的浏览器

问题描述:

我试图创建一个显示网页的JPanel。我已经到达 的阶段,我可以在那里阅读网页,但是当我显示该页面时,它看起来全是混乱 了,见下文。JPanel内置的浏览器

enter image description here

这里是源代码:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.net.*; 
import java.io.*; 

public class Browser { 
private JFrame frame; 
private JPanel panelTop; 
private JEditorPane editor; 
private JScrollPane scroll; 
private JTextField field; 
private JButton button; 
private URL url; 

public Browser(String title) { 
    initComponents(); 

    //set the title of the frame 
    frame.setTitle(title); 

    //set the default cloe op of the jframe 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //set size of frame 
    frame.setSize(800,600); 

    //add jpanel to north of jframe 
    frame.add(BorderLayout.NORTH, panelTop); 

    //add textfield and navigation button to jpanel. 
    panelTop.add(field); 
    panelTop.add(button); 

    //add scroll pane to jframe center 
    frame.add(BorderLayout.CENTER, scroll); 


    //set the frame visible 
    frame.setVisible(true); 
}//end Browser() constructor 

private void initComponents() { 
    //create the JFrame 
    frame = new JFrame(); 

    //create the JPanel used to hold the text field and button. 
    panelTop = new JPanel(); 

    //set the url 
    try { 
     url = new URL("http://www.google.co.uk"); 
    } 
    catch(MalformedURLException mue) { 
     JOptionPane.showMessageDialog(null,mue); 
    } 

    //create the JEditorPane 
    try { 
     editor = new JEditorPane(url); 

     //set the editor pane to false. 
     editor.setEditable(false); 
    } 
    catch(IOException ioe) { 
     JOptionPane.showMessageDialog(null,ioe); 
    } 

    //create the scroll pane and add the JEditorPane to it. 
    scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

    //create the JTextField 
    field = new JTextField(); 

    //set the JTextField text to the url. 
    //we're not doing this on the event dispatch thread, so we need to use 
    //SwingUtilities. 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      field.setText(url.toString()); 
     } 
    }); 

    //create the button for chanign pages. 
    button = new JButton("Go"); 

    //add action listener to the button 
    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       editor.setPage(field.getText()); 
      } 
      catch(IOException ioe) { 
       JOptionPane.showMessageDialog(null, ioe); 
      } 
     } 
    }); 
}//end initComponents() 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new Browser("Simple web browser"); 
     } 
    }); 
}//end main method. 
}//end Browser class 

有没有一些方法,我缺少读取CSS安德闪光灯等?

+1

我几乎可以肯定的JEditorPane是不是能够处理最先进的网页内容类型。 – mcfinnigan 2012-01-06 11:52:52

仅仅因为JEditorPane呈现旧的(3.2的一部分)HTML和简单的CSS不会使它成为浏览器。它仅适用于您控制的内容。

有没有一些方法,我缺少读取CSS ..

它支持简单的款式。

.. flash等?

不支持默认HTML editor kit

支持新的HTML元素可以将添加到编辑器工具包中,但您需要提供呈现Flash,..applet等任何组件。

+0

有没有其他方法可以使用? – 2012-01-06 11:53:33

+1

尝试http://djproject.sourceforge.net/ns/index.html – 2012-01-06 11:55:54

嗯,是的。通常你会使用一些Java http客户端(在这种情况下,JEditorPane能够从url获得Html响应)来获取来自HTTP地址的响应,您将获得文本,但不执行Javascript代码,也没有RIA小部件输出(如Flash,Silverlight等)。现在对于Javascript问题,您可以使用HtmlUnit(http://htmlunit.sourceforge.net/)来执行Javascript,但对于Flash小部件,您仍然失败。

但是,您尝试在此处显示的Google页面通常没有任何Flash或类似的内容。只需使用HTMLUnit从url中获取html(加上执行的javascript),并显示它。您可能需要进一步手动解析该HTML,以便微调Swing GUI。

Lobo是一个开源的Java Web浏览器。你可能想签的来源,看看那些人是怎么做的:

http://lobobrowser.org/sourcecode.jsp