博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java swing 链接_JAVA中Jtable标签设置超级链接:基于Java Swing的超链接标签和超链接按钮的实现...
阅读量:6911 次
发布时间:2019-06-27

本文共 1260 字,大约阅读时间需要 4 分钟。

希望在一个标签上设置超级链接,找到了如下的一种方法:

1.要解决的问题

基于Java Swing的超链接实现

2.自定义一个超链接标签控件[LinkLabel]

2.1.完整代码如下:

public 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(“” + text);

else

setText(“” + text);

}

public static void main(String[] args) {

JFrame jf = new JFrame(“一个超链接实现的例子-志文工作室”);

JPanel jp = new JPanel();

jp.add(new LinkLabel(“志文工作室”, “http://lzw.me”));

jf.setContentPane(jp);

jf.pack();

jf.setVisible(true);

}

}

2.2.程序运行效果预览

a2008122317188.jpg

820081223171838.jpg

3.通过一个按钮来实现超链接跳转

3.1.代码如下

try {

Desktop.getDesktop().browse(new java.net.URI(http://lzw.me));

} catch (Exception ex) {

}

转载地址:http://cjncl.baihongyu.com/

你可能感兴趣的文章
Ajax.BeginForm()实现ajax无刷新提交
查看>>
GROOVY简单语法实习
查看>>
刷新神经网络新深度:ImageNet计算机视觉挑战赛微软中国研究员夺冠
查看>>
27.OGNL与ValueStack(VS)-获取Stack Context中的信息
查看>>
MySQL数据库的事务管理
查看>>
BZOJ4631 : 踩气球
查看>>
离婚后感言
查看>>
怎么使用阿里云直播服务应用到现在主流直播平台中
查看>>
Ubuntu 安装搜狗拼音及fcitx
查看>>
JS中级 - 03:文档宽高及窗口事件(选)
查看>>
JavaScript类型转换
查看>>
路由器断网如何配置上网
查看>>
Vim保存只读模式下的修改
查看>>
转】Eclipse在线安装SVN
查看>>
Configuration
查看>>
DirectShowNet 使用摄像头录像+录音
查看>>
Oracle、SQL Server、MySQL数据类型对比
查看>>
php开n次方
查看>>
eclipse自动补全的设置
查看>>
Delphi的三目运算 ifthen 和iif
查看>>