再有代码在JEditorPane中打开类似www.baidu.com的超链接是,运行没有问题,当换成带参数的超链接时,就会提示路径不存在。感觉处理的时候没有把整个链接当成带参数链接处理。急求达人指点。
现有代码如下:
import java.applet.Applet;import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import net.java.dev.designgridlayout.DesignGridLayout;
public class Hyperlink extends Applet { public Hyperlink(){
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
jEditorPane.setContentType("text/html");
jEditorPane.setText("<html><body><a href=\"http://www.baidu.com/s?wd=李宇春\">baidu</a></body></html>");
jEditorPane.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command = "explorer.exe "
+ e.getURL().toString();
Runtime.getRuntime().exec(command);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("connection error");
}
} } });
DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid().add(jEditorPane);
}
}
现有代码如下:
import java.applet.Applet;import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import net.java.dev.designgridlayout.DesignGridLayout;
public class Hyperlink extends Applet { public Hyperlink(){
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
jEditorPane.setContentType("text/html");
jEditorPane.setText("<html><body><a href=\"http://www.baidu.com/s?wd=李宇春\">baidu</a></body></html>");
jEditorPane.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command = "explorer.exe "
+ e.getURL().toString();
Runtime.getRuntime().exec(command);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("connection error");
}
} } });
DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid().add(jEditorPane);
}
}
wd=李宇春 wd是变量名,李宇春是值。这样可以直接打开百度关于李宇春的检索结果。