Swing初学解决方法
Swing初学
我根据一篇博客编写代码,想用Swing来显示本地html文件,运行后一直提示错误:内容如下
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at javax.swing.JEditorPane.setPage(Unknown Source)
at com.unmi.HTMLView.<init>(HTMLView.java:38)
at com.unmi.HTMLView.main(HTMLView.java:69)
读取页面C:\Users\CAD\Desktop\scioa\table.htmlunknown protocol: c
java源代码
package com.unmi;
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.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class HTMLView extends JFrame implements HyperlinkListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public HTMLView() throws Exception{
setSize(640,480);
setTitle("隔叶黄莺:The Blog of Unmi");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane editorPane = new JEditorPane();
JScrollPane scrollPane = new JScrollPane();
editorPane.setEditable(false);
editorPane.addHyperlinkListener(this);
File file = new File("C:/Users/CAD/Desktop/scioa/table.html");
String path = file.getAbsolutePath();
try {
editorPane.setPage(path);
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取页面" + path +"" + e.getMessage());
}
Container container = getContentPane();
container.add(scrollPane,BorderLayout.CENTER);
}
@Override
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();
}
}
}
}
public static void main(String[] args) throws Exception{
JFrame frame = new HTMLView();
frame.setVisible(true);
}
}
找了好几个小时,还是没办法解决
求大神帮我看下,是什么原因,感激不尽
------解决方案--------------------
不要用file.getAbsolutePath()
用file.toURI().toURL().toString()
------解决方案--------------------
添加一行:
然后修改一下
------解决方案--------------------
JEditorPane本来就很渣,不能保证一定能正常解析网页,一般静态页面还好,动态页面基本无法解析
------解决方案--------------------
参考一下吧:http://blog.****.net/gtuu0123/article/details/4294352
我根据一篇博客编写代码,想用Swing来显示本地html文件,运行后一直提示错误:内容如下
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at javax.swing.JEditorPane.setPage(Unknown Source)
at com.unmi.HTMLView.<init>(HTMLView.java:38)
at com.unmi.HTMLView.main(HTMLView.java:69)
读取页面C:\Users\CAD\Desktop\scioa\table.htmlunknown protocol: c
java源代码
package com.unmi;
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.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class HTMLView extends JFrame implements HyperlinkListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public HTMLView() throws Exception{
setSize(640,480);
setTitle("隔叶黄莺:The Blog of Unmi");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane editorPane = new JEditorPane();
JScrollPane scrollPane = new JScrollPane();
editorPane.setEditable(false);
editorPane.addHyperlinkListener(this);
File file = new File("C:/Users/CAD/Desktop/scioa/table.html");
String path = file.getAbsolutePath();
try {
editorPane.setPage(path);
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取页面" + path +"" + e.getMessage());
}
Container container = getContentPane();
container.add(scrollPane,BorderLayout.CENTER);
}
@Override
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();
}
}
}
}
public static void main(String[] args) throws Exception{
JFrame frame = new HTMLView();
frame.setVisible(true);
}
}
找了好几个小时,还是没办法解决
求大神帮我看下,是什么原因,感激不尽
------解决方案--------------------
不要用file.getAbsolutePath()
用file.toURI().toURL().toString()
------解决方案--------------------
添加一行:
editorPane.add(scrollPane);
然后修改一下
container.add(editorPane, BorderLayout.CENTER);
------解决方案--------------------
JEditorPane本来就很渣,不能保证一定能正常解析网页,一般静态页面还好,动态页面基本无法解析
------解决方案--------------------
参考一下吧:http://blog.****.net/gtuu0123/article/details/4294352