JLabel超链接以正确的URL打开浏览器 [英] JLabel hyperlink to open browser at correct URL

查看:187
本文介绍了JLabel超链接以正确的URL打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java Swing创建一个可点击的标签,并且能够在桌面上打开默认浏览器并将其重定向到特定网址。我的代码能够打开浏览器,但不能将其重定向到正确的URL(加载默认主页)。我的测试代码:

I need to create a label with Java Swing that is clickable and able to open the default browser on the desktop and redirect it to a specific url. My code is able to open up the browser but not redirecting it to the correct url (the default home page is loaded). My test code:

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 import java.io.IOException;
 import java.net.*;

 public class LinkTest extends JFrame {

 public LinkTest() {
 JPanel p = new JPanel();

 JLabel link = new JLabel("Click here");
 link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 link.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() > 0) {
          if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();
                try {
                    URI uri = new URI("http://www.bbc.co.uk");
                    desktop.browse(uri);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (URISyntaxException ex) {
                    ex.printStackTrace();
                }
        }
      }
   }
});
p.add(link);
getContentPane().add(BorderLayout.NORTH, p);
 }

 public static void main(String[] args) {
  LinkTest linkTest = new LinkTest();
  linkTest.setSize(640,100);
  linkTest.show();
 }
}

如何打开默认浏览器并重定向到使用Java Swing的正确URL?

How can I have a default browser open and redirect to the correct URL with Java Swing?

推荐答案

发现问题:在Ubuntu 12.10上我安装了libgnome2并且它运行正常现在。

Found the problem: on Ubuntu 12.10 I have installed "libgnome2" and it is working perfectly now.

这篇关于JLabel超链接以正确的URL打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆