qq_38773852
qq_38773852
2017-05-19 04:46

JAVA Swing中嵌入html代码,但是一直出错

15
  • swing
  • java
  • html5
  • 界面

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class Test1 extends JFrame implements HyperlinkListener {
public Test1(){
Container c = getContentPane();
setSize(539,412);
setTitle("QQ登录界面");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    JEditorPane pane = new JEditorPane();
    pane.setContentType("text/html");
    pane.setEditable(false);
    pane.addHyperlinkListener(this);
    File file = new File("http://E://Eclipse//workspace//QQ//src//qq//QQ.html");
    String path = file.getAbsolutePath();
    try {
        pane.setPage(path);

    } catch (IOException e) {
        // TODO 自动生成的 catch 块
        //System.out.println("读取页面 " + path + " 出错. " + );
        e.printStackTrace();
    }  
    pane.setBounds(0, 0, 539, 199);
    c.add(pane);
    setVisible(true);

}

@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
    // TODO 自动生成的方法存根
    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();  
            }  
         }  
      }  

}

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    new Test1();

}

}

图片是运行结果和异常图片说明
图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐