有谁知道如何从JLabel
鼠标的开头到鼠标指针获取部分文本?例如,假设我们有一个JLabel
文本为'C:\ aaa \ bbb \ ccc'的文本。用户将鼠标指针指向字符“ bbb”,因此我想获取文本“ C:\ aaa \ bbb”。现在,当我拥有这部分文字时,可以更改其颜色。我认为将使用html。
Java Accessibility API方便地getIndexAtPoint
在AccessibleText
接口中包括一个方法,该方法将位置(例如鼠标指针的位置)转换为该位置处字符的索引:
给定局部坐标中的一个点,返回该点下字符的从零开始的索引。如果该点无效,则此方法返回-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()
将产生,只有不断返回的对象-1
从getIndexAtPoint
。
编辑:可以获取文本的一部分,而不必担心根据未显示为可见文本的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] 删除。
我来说两句