想用Java做一个小软件,将web页面嵌入软件面板中,该如何实现? 也就是想在软件像浏览器一样展示html源码或者某一URL的网页页面?有什么第三方包可以使用吗? 

解决方案 »

  1.   

    javax.swing.JEditorPane
    可以实现html的展示,支持HTML 3.2
      

  2.   

    看看能不能帮上忙public class HelpDialog
        extends JDialog
        implements HyperlinkListener {
      JScrollPane ScrollPane = new JScrollPane();
      JEditorPane HelpPane = new JEditorPane();
      Border border1;
      JPanel Panel1 = new JPanel();
      JButton Close = new JButton();
      Border border2;  public HelpDialog(JFrame frame) throws HeadlessException {
        super(frame, true);
        try {
          jbInit();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation( (int) (screenSize.width - 420) / 2,
                         (int) (screenSize.height - 360) / 2);
        this.setResizable(false);
      }  private void jbInit() throws Exception {
        border2 = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.lightGray,1),BorderFactory.createEmptyBorder(2,10,2,10));
        this.setSize(new Dimension(420, 360));
        this.setTitle("Help");
        border1 = BorderFactory.createEmptyBorder();    URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
        URL url = null;
        url = urlLoader.findResource("doc/help.htm");
        HelpPane.setPage(url);
        HelpPane.setEditable(false);
        HelpPane.addHyperlinkListener(this);
        ScrollPane.setHorizontalScrollBarPolicy(JScrollPane.
                                                HORIZONTAL_SCROLLBAR_NEVER);
        ScrollPane.setVerticalScrollBarPolicy(JScrollPane.
                                              VERTICAL_SCROLLBAR_AS_NEEDED);
        ScrollPane.setBorder(border1);
        Close.setBackground(Color.white);
        Close.setBorder(border2);
        Close.setActionCommand("jButton1");
        Close.setText("Close");
        Close.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            setVisible(false);
          }
        });    Panel1.setBackground(Color.white);
        this.getContentPane().add(ScrollPane, BorderLayout.CENTER);
        this.getContentPane().add(Panel1,  BorderLayout.SOUTH);
        ScrollPane.getViewport().add(HelpPane, null);
        Panel1.add(Close, null);
      }  public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          JEditorPane pane = (JEditorPane) e.getSource();
          if (e instanceof HTMLFrameHyperlinkEvent) {
            HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
            HTMLDocument doc = (HTMLDocument) pane.getDocument();
            doc.processHTMLFrameHyperlinkEvent(evt);
          }
          else {
            try {
              pane.setPage(e.getURL());
            }
            catch (Throwable t) {
              t.printStackTrace();
            }
          }
        }
      }}
      

  3.   

    用JEditorPane只能看html,可以实现link功能,但是script不能用,和真正的浏览器还差好多,建议用jdic(JDesktop Integration Components)
    参考页面和演示demo都有
    https://jdic.dev.java.net/