ActionListener为JTextArea中的特定文本? [英] ActionListener for a specific text inside a JTextArea?

查看:201
本文介绍了ActionListener为JTextArea中的特定文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个聊天组件,它有一个 JTextArea
现在,如何为特定文本添加一个类似ActionListener的事件(如student:// xxxx)?



所以当我点击那个文本(学生:// xxxx)会发生什么事情。
谢谢。

解决方案

这里尝试这个小程序,尝试点击学生的开始://,这将弹出一个消息对话框

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;

public class TextAreaExample extends JFrame
{
private JTextArea tarea = new JTextArea(10,10);
private JTextField tfield = new JTextField(10);

private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tarea.setText(Hello there\\\
);
tarea.append(Hello student://);
JScrollPane scroll = new JScrollPane(tarea);

tfield.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
tarea.append(tfield.getText() +\\\
);
}
});

tarea.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
System.out.println(X:+ x);
System.out.println(Y:+ y);
int startOffset = tarea.viewToModel(new Point(x,y));
System.out.println(Start Offset:+ startOffset);
String text = tarea.getText() ;
int searchLocation = text.indexOf(student://,startOffset);
System.out.println(Search Location:+ searchLocation);
if(searchLocation == startOffset)
JOptionPane.showMessageDialog(TextAreaExample.this,BINGO你找到我);
}
});

getContentPane()。add(scroll,BorderLayout.CENTER);
getContentPane()。add(tfield,BorderLayout.PAGE_END);
pack();
setLocationByPlatform(true);
setVisible(true);
}

public static void main(String ... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TextAreaExample()。createAndDisplayGUI();
}
});
}
}


I have in my app a chat component which has a JTextArea on it. Now, how can I add an ActionListener-like event for a specific text (like student://xxxx)?

So when I click on that text (student://xxxx) something will happen. Thank you.

解决方案

Here try this small program, try to click at the start of student://, that will pop up a message Dialog

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaExample extends JFrame
{
    private JTextArea tarea =  new JTextArea(10, 10);
    private JTextField tfield = new JTextField(10);

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tarea.setText("Hello there\n");
        tarea.append("Hello student://");
        JScrollPane scroll = new JScrollPane(tarea);

        tfield.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                tarea.append(tfield.getText() + "\n");
            }
        });

        tarea.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                int x = me.getX();
                int y = me.getY();
                System.out.println("X : " + x);
                System.out.println("Y : " + y);
                int startOffset = tarea.viewToModel(new Point(x, y));
                System.out.println("Start Offset : " + startOffset);
                String text = tarea.getText();
                int searchLocation = text.indexOf("student://", startOffset);
                System.out.println("Search Location : " + searchLocation);
                if (searchLocation == startOffset)
                    JOptionPane.showMessageDialog(TextAreaExample.this, "BINGO you found me.");
            }
        });

        getContentPane().add(scroll, BorderLayout.CENTER);
        getContentPane().add(tfield, BorderLayout.PAGE_END);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextAreaExample().createAndDisplayGUI();
            }
        });
    }
}

这篇关于ActionListener为JTextArea中的特定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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