这只是一个样例,没有实现完整,只能响应点击超链的行为,不能响应点击按钮的事件请求
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Exec {
public static void main(String[] args) {
DemoWindow dw = new DemoWindow("演示");
Toolkit theKit = dw.getToolkit();
Dimension wndSize = theKit.getScreenSize();
dw.setBounds(wndSize.width / 4, wndSize.height / 4, wndSize.width / 2,
wndSize.height / 2);
dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dw.setVisible(true);
}
}
class DemoWindow extends JFrame {
JEditorPane jep = new JEditorPane();
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp = new JScrollPane(jep, v, h);
public DemoWindow(String title) {
super(title);
jep.setEditable(false);
jep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
jep.setPage(evt.getURL());
} catch (Exception e) {
}
}
}
});
try {
jep.setContentType("text/html");
jep
.setText("<table width='100%'><tr><td><div align='center'><a href='http://www.baidu.com/'>打开网站</A></div></td></tr></table>");
} catch (Exception e) {
}
add(jsp);
}
}
[此贴子已经被作者于2010-12-12 08:26:50编辑过]