简体   繁体   English

如何识别JTextPane中所选文本的大小是否不同?

[英]How to identify if the selected text in JTextPane are of different sizes?

I am developing a editor where I have a combobox for font size. 我正在开发一个编辑器,其中有一个用于显示字体大小的组合框。 User can use it to alter the text size. 用户可以使用它来更改文本大小。

I have also implemented the caret listener which tells me the size of selected text and updates the combobox accordingly. 我还实现了插入符号侦听器,该侦听器告诉我所选文本的大小并相应地更新组合框。 Now if the user selects the text of two different sizes I want to populate blank value in Combobox. 现在,如果用户选择两种不同大小的文本,我想在组合框中填充空白值。 But I am unable to do that with Caret listener as It gives me size of the first text. 但是我无法使用Caret侦听器来执行此操作,因为它使我可以容纳第一段文字的大小。

Eg: If my text is "HI". 例如:如果我的文字是“ HI”。 Here H is of size 12 and I is of size 22. Now when I select "HI" caret listener gives me the value of either 12 or 22. 这里H的大小为12,I的大小为22。现在,当我选择“ HI”时,插入号侦听器会为我提供12或22的值。

Here is the sample code: 这是示例代码:

StyledDocument doc = pane.getStyledDocument();
MutableAttributeSet fontSizeStyle = ((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
int fontSize = StyleConstants.getFontSize(fontSizeStyle);

Below code would loop through the selected text in Jtextpane and get each character's size. 下面的代码将循环遍历Jtextpane中的选定文本,并获取每个字符的大小。

String selectedText = pane.getSelectedText();
int k = pane.getSelectionStart();

for(int i=0; i< selectedText.length(); i++) {
   AttributeSet fontSize = doc.getCharacterElement(k).getAttributes();                       
   System.out.println("fontSize:"+StyleConstants.getFontSize(fontSize));
   k++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM