是否可以用Java创建程序来创建要在Chrome中链接的文本? [英] Is it possible to create programs in Java that create text to link in Chrome?

查看:129
本文介绍了是否可以用Java创建程序来创建要在Chrome中链接的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个长期问题道歉。

I apologize for the long question.

前几天我正在浏览一个论坛,我看到一些链接到youtube和其他网站的文本。
我必须始终突出显示然后复制并粘贴或右键单击谷歌浏览器中的转到。

I was browsing a forum the other day and I saw a few pieces of text that were linking to youtube and other sites. I had to always highlight and then copy and paste or right click "go to" in google chrome browser.

由于我一直在玩Java,我想到制作自己的小程序,它会给出一个带有地址的文本的链接。例如,如果我说嘿,看看这个视频,我前几天看到'www.youtube.com'我希望youtube部分可以点击。

Since I've been playing with Java a little bit, I thought about making my own little program that will give a link to text that has an address . For example if I said "hey, check this video out I saw the other day 'www.youtube.com' " I'd want the youtube part to be clickable.

有人可以告诉我这件事是否可能,如果是的话,我必须使用哪些库,最后,我怎样才能找到所有库的列表java中的导入和库?

Could anybody tell me if such a thing is possible and if it is, what libraries would I have to use for this and lastly, how can I find a list of all imports and libraries in java?

谢谢。

推荐答案

JEditorPane 并添加 HyperLinkListener 检测点击网址。

Use HTML in JEditorPane and add HyperLinkListener to detect click on URLs.

比使用桌面API 用URL打开默认浏览器。

Than use Desktop API to open default browser with the URL.

类似:

import java.awt.Desktop;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class Test {

    public static void main(String[] argv) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JEditorPane jep = new JEditorPane();
                jep.setContentType("text/html");//set content as html
                jep.setText("Welcome to <a href='http://stackoverflow.com/'>StackOverflow</a>.");

                jep.setEditable(false);//so its not editable
                jep.setOpaque(false);//so we dont see whit background

                jep.addHyperlinkListener(new HyperlinkListener() {
                    @Override
                    public void hyperlinkUpdate(HyperlinkEvent hle) {
                        if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
                            System.out.println(hle.getURL());
                            Desktop desktop = Desktop.getDesktop();
                            try {
                                desktop.browse(hle.getURL().toURI());
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                });


                JFrame f = new JFrame("HyperlinkListener");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(jep);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

这篇关于是否可以用Java创建程序来创建要在Chrome中链接的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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