超级连接的问题?在线等。。。。。
编译下面的class,再拷贝一个index.html文件,到class生成的目录下,运行,即可看到:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.prefs.*;
import javax.swing.*;
import javax.swing.border.*;
public class a extends JFrame {
public static void main(String[] args) throws Exception {
JFrame a =new a();
a.setSize(400, 300);
a.setVisible(true);
}
public a() {
try {
URL url = null;
// System.getProperty("user.dir") +
// System.getProperty("file.separator");
String path = null;
try {
path = "/index.html";
url = getClass().getResource(path);
} catch (Exception e) {
System.err.println("Failed to open " + path);
url = null;
}
if(url != null) {
JEditorPane html = new JEditorPane(url);
html.setEditable(false);
JScrollPane scroller = new JScrollPane(html);
getContentPane().add(scroller, BorderLayout.CENTER);
}
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e);
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}
}
上面的可打开index.html但不可联接,我笨不知道把下面如何加上去好来完成联接
1,语句:
html.addHyperlinkListener(createHyperLinkListener());
给JEditorPane添加了一个链接的监听器,该监听器可以监听在链接上发生的进入、点击、退出三种事件;
2,下面的方法是处理链接事件的:
public HyperlinkListener createHyperLinkListener() {
return new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (e instanceof HTMLFrameHyperlinkEvent) {
((HTMLDocument)html.getDocument()).processHTMLFrameHyperlinkEvent(
(HTMLFrameHyperlinkEvent)e);
} else {
try {
html.setPage(e.getURL());
} catch (IOException ioe) {
System.out.println("IOE: " + ioe);
}
}
}
}
};
}
问题点数:0、回复次数:0Top




