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

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

问题描述

对于这么长的问题,我深表歉意.

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

因为我一直在玩 Java,所以我想制作自己的小程序,该程序将提供指向具有地址的文本的链接.例如,如果我说嘿,看看这个视频,我前几天看到了‘www.youtube.com’",我希望 youtube 部分是可点击的.

有人能告诉我这样的事情是否可能,如果可能的话,我必须为此使用哪些库,最后,我如何找到 Java 中所有导入和库的列表?

谢谢.

解决方案

在 .");jep.setEditable(false);//所以它不可编辑jep.setOpaque(false);//所以我们看不到白色背景jep.addHyperlinkListener(new HyperlinkListener() {@覆盖public void hyperlinkUpdate(HyperlinkEvent hle) {if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {System.out.println(hle.getURL());桌面桌面 = Desktop.getDesktop();尝试 {desktop.browse(hle.getURL().toURI());} 捕捉(异常前){ex.printStackTrace();}}}});JFrame f = new JFrame("HyperlinkListener");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.add(jep);f.pack();f.setVisible(true);}});}}

I apologize for the long question.

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.

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.

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?

Thanks.

解决方案

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

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

Something like:

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天全站免登陆