笨笨的问,在应用程序中能够显示超链接的文本吗?

xiaoyanchang 2003-10-14 12:17:59
我想在我的应用程序中显示超链接的文本,就像我们网页上的那样,鼠标点后连接到相应的网页上,有没有办法啊?不是非要用jsp之类的把?
...全文
42 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lymkelly 2003-10-16
  • 打赏
  • 举报
回复
关注
Dekn 2003-10-16
  • 打赏
  • 举报
回复
下面我给你的答案,看你在JAVA开发者上也在问这个问题。

要注意以下几点:
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);
}
}

xiaoyanchang 2003-10-15
  • 打赏
  • 举报
回复
不在JEditorPane中显示网页,是用类似下边的代码在IE中显示网页:
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);
不对
xiaoyanchang 2003-10-15
  • 打赏
  • 举报
回复
按照Dekn()的方法能显示出来了,
我要的效果是:
比如鼠标点击Dekn文本,然后我就可以知道它对应的url,调用IE就可以浏览页面了
怎么判断我鼠标点的文本是什么呢?
wangyanqiu 2003-10-15
  • 打赏
  • 举报
回复


up
cd1860 2003-10-15
  • 打赏
  • 举报
回复
Dekn() 的正确!
stamp80 2003-10-15
  • 打赏
  • 举报
回复
这样的浏览器用的就是dekn提到的JEditorPane
stamp80 2003-10-15
  • 打赏
  • 举报
回复
在类似ie这样的浏览器中,你想通过超连接得到下面网页的地址,也可以用下面的方法
你只要编写一个类实现HyperlinkListener接口,在类中实现hyperlinkUpdate(HyperlinkEvent event)方法
public void hyperlinkUpdate(HyperlinkEvent event){
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
URL url = event.getURL();
}
然后将这个url转成string类型就成了,它就是下一个网页的地址。
LuZhou 2003-10-14
  • 打赏
  • 举报
回复
在Win32程序中,通过Win SDK可以做到,在java程序中通过swing可以实现。
huangry 2003-10-14
  • 打赏
  • 举报
回复
当然可以的 你难道没有看到WebStart界面上面就有链接吗?
Mars_wx 2003-10-14
  • 打赏
  • 举报
回复
其实JEditorPane支持HTML的,editorPane.setPage(你的HTML文件的路径)
seaman0916 2003-10-14
  • 打赏
  • 举报
回复
是呀! Java的Swing控件里是支持HTML语法的!
Dekn 2003-10-14
  • 打赏
  • 举报
回复
显示在jbutton上,用HTML语法就可以,不过要写全。就是要写上<html><body></body></html>.如
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>");

你可以参考上面的代码。试一试

tripofdream 2003-10-14
  • 打赏
  • 举报
回复
javax.swing.text.html.*
xiaoyanchang 2003-10-14
  • 打赏
  • 举报
回复
用swing怎么实现,能具体一点吗?我从数据库里读数据,原来做的用JTextArea,只能显示文本,想做成超链接的,该用什么?,谢谢

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧