当然可以的 你难道没有看到WebStart界面上面就有链接吗?
解决方案 »
- 学习自定义类加载器,运行时候报错,求解!!!
- 怎样获取手机屏幕大小?
- 为什么socket的outputstream关闭后socket的状态也是关闭的呢?
- 通过什么软件,能将文本转换为二进制各市进行察看,比如UtralEdit 等工具!___特急
- 怎样把netBeans里的工程导入eclipse中去
- java事务很ORACLE存储过程事务隔离级别,哪个起作用?
- java调用ocx控件问题 怎么连接cti服务器 在先等谢谢
- JAVA简单问题
- java里的package怎么用的?
- Javascript中怎样调用控件?
- 求jcreate v2.5的注册码或注册机,分不是蛮多,但请帮帮忙!!
- 三天了,,三天没睡了,就因为这个程序,请各位帮帮忙看看!!!!!!
jbutton1.setText("<html><body><a href = http://www.163.com>www.163.com</a></body></html>");
不过,前些日子,我研究了JEditorPane,可以让其直接显示HTML
如下:jEditorPane1.setContentType("text/html");
jEditorPane1.setText("<html><body><a href = http://www.163.com> Dekn </a></body></html>");你可以参考上面的代码。试一试
我要的效果是:
比如鼠标点击Dekn文本,然后我就可以知道它对应的url,调用IE就可以浏览页面了
怎么判断我鼠标点的文本是什么呢?
你只要编写一个类实现HyperlinkListener接口,在类中实现hyperlinkUpdate(HyperlinkEvent event)方法
public void hyperlinkUpdate(HyperlinkEvent event){
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
URL url = event.getURL();
}
然后将这个url转成string类型就成了,它就是下一个网页的地址。
String command="c:\\Program Files\\Internet Explorer\\IEXPLORE.exe "+s;
Runtime.getRuntime().exec(command);
要得到的就是点击的文本对应的url,
我用URL url = event.getURL();
s=url.toString();
String command="c:\\Program Files\\Internet Explorer\\IEXPLORE.exe "+s;
Runtime.getRuntime().exec(command);
不对
1、想要使JEditorPane支持LINK,必须设置其editable为false
2、为JEditorPane加上HyperlinkListener,并从中判断其类型。即点击看下面代码:jEditorPane1.addHyperlinkListener(new Frame1_jEditorPane1_hyperlinkAdapter(this));
contentPane.add(jEditorPane1, BorderLayout.CENTER);jEditorPane1.setContentType("text/html");
jEditorPane1.setText("<html><body><a href = http://www.163.com> Dekn </a></body></html>");//要想JEditorPane支持链接,必须设置editable为false
jEditorPane1.setEditable(false);void jEditorPane1_hyperlinkUpdate(HyperlinkEvent e) {
System.err.println(e.getURL());
//如果用户点击了link
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command="c:\\Program Files\\Internet Explorer\\IEXPLORE.exe "+e.getURL().toString();
Runtime.getRuntime().exec(command);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println("connection error");
}
}
}
下面是我写这个范例的所有代码。
package untitled3;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.Runtime;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author Dekn
* @version 1.0
*/public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JEditorPane jEditorPane1 = new JEditorPane(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jEditorPane1.setText("jEditorPane1");
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(458, 316));
this.setTitle("Frame Title");
jEditorPane1.addHyperlinkListener(new Frame1_jEditorPane1_hyperlinkAdapter(this));
contentPane.add(jEditorPane1, BorderLayout.CENTER); jEditorPane1.setContentType("text/html");
jEditorPane1.setText("<html><body><a href = http://www.163.com> Dekn </a></body></html>"); //要想JEditorPane支持链接,必须设置editable为false
jEditorPane1.setEditable(false);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jEditorPane1_hyperlinkUpdate(HyperlinkEvent e) {
System.err.println(e.getURL()); //如果用户点击了link
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
String command="c:\\Program Files\\Internet Explorer\\IEXPLORE.exe "+e.getURL().toString();
Runtime.getRuntime().exec(command);
}
catch (Exception ex) {
ex.printStackTrace();
System.err.println("connection error");
}
}
}
}class Frame1_jEditorPane1_hyperlinkAdapter implements javax.swing.event.HyperlinkListener {
Frame1 adaptee; Frame1_jEditorPane1_hyperlinkAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void hyperlinkUpdate(HyperlinkEvent e) {
adaptee.jEditorPane1_hyperlinkUpdate(e);
}
}