比如说在HTML里面用<a href=www.qq.com target="blank">点击进去QQ官网</a>
比如我希望这个功能在Swing窗口程序里也同样实现这功能,在JAVA程序代码里该如何搞哟?
/**
* @(#)MyTest.java
*
*
* @author if
* @version 1.00 2007/3/24
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyTest extends JFrame{
public MyTest() {
setTitle("link");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jbutton=new JButton();
jbutton.setText("goto bc-cn");
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Runtime.getRuntime().exec("explorer.exe http://www.bc-cn.net");
}catch(Exception ex){}
}
});
getContentPane().add(jbutton);
pack();
}
public static void main(String[] args) {
MyTest frame=new MyTest();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*Swing中实现超连接,注意:需要JDK1.6
*@author Eastsun
*/
public class SupperLink{
public static void main(String[] args){
JFrame frame =new JFrame(\"SupperLink\");
JLabel label =new LinkLabel(\"欢迎访问 Eastsun's blog^_^\",\"http://eastsun.javaeye.com\");
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
/**
*LinkLabel,一个实现超连接的Label.
*使用方法: label =new LinkLabel(\"在label上现示的文本\",\"点击文本时打开的url地址\");
*/
class LinkLabel extends JLabel{
private String text,url;
private boolean isSupported;
public LinkLabel(String text,String url){
this.text =text;
this.url =url;
try{
this.isSupported = Desktop.isDesktopSupported()&&Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
}catch(Exception e){
this.isSupported = false;
}
setText(false);
addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
setText(isSupported);
if(isSupported) setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e){
setText(false);
}
public void mouseClicked(MouseEvent e){
try{
Desktop.getDesktop().browse(new java.net.URI(LinkLabel.this.url));
}catch(Exception ex){
}
}
});
}
private void setText(boolean b){
if(!b) setText(\"<html><font color=black>\"+text);
else setText(\"<html><font color=blue><u>\"+text);
}
}