从JTextPane中的任何偏移量获取样式 [英] Getting style from any offset in JTextPane

查看:206
本文介绍了从JTextPane中的任何偏移量获取样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得 Style ,样式名称或甚至比较某个位置的 Style JTextPane 与插入时我给出文字的样式?因为我的目的,我创建了自定义 JTextPane StyledDocument DocumentFilter 。所以我可以选择 Style 来用于说普通字母和另一种用于数字的样式。我还切换按钮,当切换设置 DocumentFilter 时,以不同方式格式化数字,而不是定期切换数字格式,所以最后你无法区分哪些数字受到影响到 JTextPane的getText()方法。因此,唯一的方法是比较我将常规和特殊数字样式作为常量的样式。我需要提出的是如何为每个角色获得Style。

Is there a way to get Style, a style name or just even compare whether Style at a certain position of JTextPane with the style I gave the text when inserting? Because for my purpose I created custom JTextPane, StyledDocument and DocumentFilter. So I could choose Style to be used for say regular letters and another Style for numbers. I have also toggle button which while toggled sets DocumentFilter to format numbers differently and while not toggled numbers format regularly so at the end you can't distinguish which numbers have been affected just according to JTextPane's getText() method. Therefore the only way would be to compare styles which I have both regular and special number style as constants. Only thing I need to come up with is how to get Style for each character.

我知道有 JTextPane的从插入符号位置获取AttributeSet的方法,名为 getCharacterAttributes(),但我认为这对我的问题毫无用处。

I know there is JTextPane's method to get AttributeSet from the caret's position called getCharacterAttributes() but I think it's no use for my issue.

是否有必要包含代码示例?我不认为这很难想象。如果你想要我,我会把它包括在内。

Is it necessary to include code example? I don't think it's difficult to imagine. If you want me I will include it though.

任何输入都会受到赞赏。谢谢!

Any input would be appreciated. Thanks!

推荐答案

尝试调用 StyledDocument.getCharacterElement(pos) 获取该位置的字符元素,然后调用 Element.getAttributes() 获取其属性设置。

Try calling StyledDocument.getCharacterElement(pos) to get the character element at that position and then call Element.getAttributes() to get its attribute set.

AttributeSet 包含可以使用 StyleConstants

这篇关于从JTextPane中的任何偏移量获取样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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