如何根据鼠标指针从JLabel获取文本的一部分

警察

有谁知道如何从JLabel鼠标的开头到鼠标指针获取部分文本例如,假设我们有一个JLabel文本为'C:\ aaa \ bbb \ ccc'的文本。用户将鼠标指针指向字符“ bbb”,因此我想获取文本“ C:\ aaa \ bbb”。现在,当我拥有这部分文字时,可以更改其颜色。我认为将使用html。

奥尔登

Java Accessibility API方便地getIndexAtPointAccessibleText接口中包括一个方法,方法将位置(例如鼠标指针的位置)转换为该位置处字符的索引:

给定局部坐标中的一个点,返回该点下字符的从零开始的索引。如果该点无效,则此方法返回-1。

这是一个使用此方法来获取您要求的字符串部分的测试程序:

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.accessibility.AccessibleText;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JLabelMouseDemo {
    private static String labelText = "<html>C:\\aaa\\bbb\\ccc</html>";
    private static JLabel label;
    private static JLabel substringDisplayLabel;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        label = new JLabel(labelText);
        label.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                AccessibleText accessibleText =
                        label.getAccessibleContext().getAccessibleText();
                Point p = e.getPoint();
                int index = accessibleText.getIndexAtPoint(p);
                if (index >= 0) {
                    // The index is with respect to the actually displayed
                    // characters rather than the entire HTML string, so we
                    // must add six to skip over "<html>", which is part of
                    // the labelText String but not actually displayed on
                    // the screen. Otherwise, the substrings could end up
                    // something like "tml>C:\aaa"
                    index += 6;

                    // Strangely, in my testing, index was a one-based index
                    // (for example, mousing over the 'C' resulted in an
                    // index of 1), but this makes getting the part of the
                    // string up to that character easier.
                    String partOfText = labelText.substring(0, index);

                    // Display for demonstration purposes; you could also
                    // figure out how to highlight it or use the string or
                    // just the index in some other way to suit your needs.
                    // For example, you might want to round the index to
                    // certain values so you will line up with groups of
                    // characters, only ever having things like
                    // "C:\aaa\bbb", and never "C:\aaa\b"
                    substringDisplayLabel.setText(partOfText);
                }
            }
        });
        frame.add(label);
        substringDisplayLabel = new JLabel();
        frame.add(substringDisplayLabel, BorderLayout.SOUTH);
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

实际上获取与特定JLabel对应的AccessibleText类型的对象可能并不总是有效:据我所知,只有在JLabel显示HTML文本时才有可能。JLabel来源似乎也支持这一点

public AccessibleText getAccessibleText() {
            View view = (View)JLabel.this.getClientProperty("html");
            if (view != null) {
                return this;
            } else {
                return null;
            }
        }

我没有声称完全理解该代码中发生的事情,或者为什么非HTML文本无法使用可访问性,但是当JLabel包含纯文本而不是HTML文本时,我的测试程序无法正常工作:label.getAccessibleContext().getAccessibleText()会返回null,并使用强制铸造(AccessibleText) label.getAccessibleContext()将产生,只有不断返回的对象-1getIndexAtPoint


编辑:可以获取文本的一部分,而不必担心根据未显示为可见文本的HTML标记的位置来调整索引。您只需要在标签上维护字符串的两个副本:一个仅包含要显示的字符(rawText在下面的示例中),这些字符将根据索引进行切片,另一个包含格式化的HTML版本,该版本实际上将用作标签的文本(以下结果formatLabelText)。由于getIndexAtPoint返回的索引仅与显示的字符有关,因此在第二个示例中,比我的原始字符串更容易获得所需的子字符串。对进行的唯一调整index是将其四舍五入,以使突出显示的文本与反斜杠分隔的组对齐。

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.accessibility.AccessibleText;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JLabelMouseHighlightDemo {
    private static String rawText = "C:\\aaa\\bbb\\ccc";
    private static JLabel label;

    private static String formatLabelText(int index) {
        if (index < 0) {
            index = 0;
        }
        if (index > rawText.length()) {
            index = rawText.length();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<font color='red'>");
        sb.append(rawText.substring(0, index));
        sb.append("</font>");
        sb.append(rawText.substring(index));
        sb.append("</html>");
        return sb.toString();
    }

    private static int roundIndex(int index) {
        // This method rounds up index to always align with a group of
        // characters delimited by a backslash, so the red text will be
        // "C:\aaa\bbb" instead of just "C:\aaa\b".
        while (index < rawText.length() && rawText.charAt(index) != '\\') {
            index++;
        }
        return index;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        label = new JLabel(formatLabelText(0));
        label.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                AccessibleText accessibleText =
                        label.getAccessibleContext().getAccessibleText();
                Point p = e.getPoint();
                int index = accessibleText.getIndexAtPoint(p);
                index = roundIndex(index);
                label.setText(formatLabelText(index));
            }
        });
        frame.add(label);
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Python在Selenium和Web驱动程序中获取文本的一部分

如何在鼠标悬停的位置显示模糊图像的一部分?

以粗体显示JLabel的一部分

如何获取网页中包含的文本并将其作为页面标题的一部分?

如何获取另一个结构的一部分的结构数组(或指针)?在C

使用Python,如何根据匹配的部分替换路径的一部分?

使用lxml标记文本的一部分

python:如何通过鼠标事件删除轮廓图的一部分(matplotlib)

XPath-仅获取属性值或文本节点的一部分

如何获取文本的一部分,包含短语/单词的字符串?

如何通过终端获取文本文件的一部分

如何在jQuery中获取文本框ID的一部分

如何获取当前的一部分URL?

Matlab:获取文本文件一部分的行数

仅获取文本文件的一部分

使用正则表达式在div内获取文本的一部分

更改JLabel的一部分的字体?

仅从Xpath提取文本的一部分

如何删除获取ID的一部分

如何在CustomControl的一部分上获取鼠标位置

根据名称的一部分读取 R 中的文本文件列表

Wpf:如何根据百分比为矩形的一部分着色?

根据变量获取 django 模型的一部分

如何根据值的一部分从表中获取数据?

如何获取 URL 的一部分?

我如何查询文本的一部分并使用猫鼬获取全部内容?

仅对 TextForm 文本的一部分着色

如何删除文本的一部分

如何根据每行的一部分按字母顺序对文本行进行排序?