如何在 JTextArea 中添加可点击的 URL?

发布于 2024-11-07 01:21:43 字数 118 浏览 4 评论 0原文

我正在编写一个应用程序,并使用 JTextArea 来显示一些文本。现在我想在文本区域中显示一些可点击的 URL 以及普通文本,并且我希望如果用户单击该 URL,则该 URL 引用的网页应在新的 Web 浏览器窗口中打开。

I am writing an application and in that I am using JTextArea to display some text. Now I want to show some clickable URL in text area along with the normal text and I want if user click on the URL then the web page that URL referring to should open in new web browser window.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掌心的温暖 2024-11-14 01:21:43

将 JEditorPane 与 HTMLEditorKit 或 JTextPane 结合使用并将内容类型设置为“text/html”

Use JEditorPane with HTMLEditorKit or JTextPane and set content type to "text/html"

请帮我爱他 2024-11-14 01:21:43

..引用的 URL 应在新的 Web 浏览器窗口中打开。

// 1.6+
Desktop.getDesktop().browse(URI);

..url referring to should open in new web browser window.

// 1.6+
Desktop.getDesktop().browse(URI);
雨落□心尘 2024-11-14 01:21:43

以下是从 JTextArea 打开链接的示例:

                JTextArea jtxa = new JTextArea(25,100);
                JScrollPane jsp = new JScrollPane(jtxa);
                JPanel jp = new JPanel();
                jp.add(jsp);
                jp.setSize(100,50);

                jtxa.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent me)
                    {
                        if(me.getClickCount()==2) //making sure there was a double click
                        {
                            int x = me.getX();
                            int y = me.getY();

                            int startOffset = jtxa.viewToModel(new Point(x, y));//where on jtextarea click was made
                            String text = jtxa.getText();
                            int searchHttp = 0;
                            int wordEndIndex = 0;
                            String[] words = text.split("\\s");//spliting the text to words. link will be a single word

                            for(String word:words)
                            {
                                if(word.startsWith("https://") || word.startsWith("http://"))//looking for the word representing the link
                                {
                                    searchHttp = text.indexOf(word);
                                    wordEndIndex = searchHttp+word.length();
                                    if(startOffset>=searchHttp && startOffset<=wordEndIndex)//after the link word was found, making sure the double click was made on this link
                                    {
                                        try
                                        {
                                            jtxa.select(searchHttp, wordEndIndex);
                                            desk.browse(new URI(word)); //opening the link in browser. Desktop desk = Desktop.getDesktop();
                                        }
                                        catch(Exception e)
                                        {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }                           
                        }

                    }
                });

Here is an example of opening links from JTextArea:

                JTextArea jtxa = new JTextArea(25,100);
                JScrollPane jsp = new JScrollPane(jtxa);
                JPanel jp = new JPanel();
                jp.add(jsp);
                jp.setSize(100,50);

                jtxa.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent me)
                    {
                        if(me.getClickCount()==2) //making sure there was a double click
                        {
                            int x = me.getX();
                            int y = me.getY();

                            int startOffset = jtxa.viewToModel(new Point(x, y));//where on jtextarea click was made
                            String text = jtxa.getText();
                            int searchHttp = 0;
                            int wordEndIndex = 0;
                            String[] words = text.split("\\s");//spliting the text to words. link will be a single word

                            for(String word:words)
                            {
                                if(word.startsWith("https://") || word.startsWith("http://"))//looking for the word representing the link
                                {
                                    searchHttp = text.indexOf(word);
                                    wordEndIndex = searchHttp+word.length();
                                    if(startOffset>=searchHttp && startOffset<=wordEndIndex)//after the link word was found, making sure the double click was made on this link
                                    {
                                        try
                                        {
                                            jtxa.select(searchHttp, wordEndIndex);
                                            desk.browse(new URI(word)); //opening the link in browser. Desktop desk = Desktop.getDesktop();
                                        }
                                        catch(Exception e)
                                        {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }                           
                        }

                    }
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文