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

Java 如何在JTextArea中添加可点击的URL?,java,swing,url,jtextarea,Java,Swing,Url,Jtextarea,我正在编写一个应用程序,我正在使用JTextArea显示一些文本。现在,我想在文本区域中显示一些可单击的URL以及普通文本,如果用户单击该URL,则URL引用的网页应在新的web浏览器窗口中打开。将JEditorPane与HTMLEditorKit或JTextPane一起使用,并将内容类型设置为“text/html” …引用的url应在新的web浏览器窗口中打开 以下是从JTextArea打开链接的示例: JTextArea jtxa = new JTextAre

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

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

…引用的url应在新的web浏览器窗口中打开


以下是从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();
                                        }

                                    }
                                }
                            }                           
                        }

                    }
                });
JTextArea jtxa=新的JTextArea(25100);
JScrollPane jsp=新的JScrollPane(jtxa);
JPanel jp=新的JPanel();
jp.add(jsp);
jp.setSize(100,50);
jtxa.addMouseListener(新的MouseAdapter()
{
公共无效mouseClicked(MouseEvent me)
{
if(me.getClickCount()==2)//确保双击
{
int x=me.getX();
int y=me.getY();
int startOffset=jtxa.viewToModel(新点(x,y));//在jtextarea上单击的位置
String text=jtxa.getText();
int searchHttp=0;
int-wordEndIndex=0;
String[]words=text.split(\\s”);//将文本拆分为单词。链接将是单个单词
for(字符串字:字)
{
if(word.startsWith(“https://”)word.startsWith(“http://”)查找表示链接的单词
{
searchHttp=text.indexOf(word);
wordEndIndex=searchHttp+word.length();

if(startOffset>=searchHttp&&startOffset查看一个简单的示例。)我会试试,但我想知道用JTextarea是否可以实现此功能?@Sandy:恐怕用JTextarea是不可能的。这只允许您将html内容添加到JTextPane/jEditorPane。我猜您希望文本转换为clickabe超链接(如果可以的话)在你打字的时候。也许你可以详细说明一下你答案中的关键点。这可以让别人更容易理解。
                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();
                                        }

                                    }
                                }
                            }                           
                        }

                    }
                });