ColorPane - 可以抓住不同角色的字符串? [英] ColorPane - grab strings of different character possible?

查看:126
本文介绍了ColorPane - 可以抓住不同角色的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个给我的旧项目,它目前使用java swing并且有一个基本的gui。它有一个扩展Jtextpane的ColorPane来改变所选文本的颜色。

I'm currently working on a old project that was given to me, it currently use java swing and has a basic gui. It has a ColorPane that extends Jtextpane to change colors of selected text.

它使用这种方法

  public void changeSelectedColor(Color c) {
      changeTextAtPosition(c, this.getSelectionStart(), this.getSelectionEnd());
  }

假设string =Hello World!你好颜色是绿色世界是黑色的。我如何从Jtextpane中获取基于其颜色的Hello out。我尝试了一种笨重的方式,只是存储所选择的单词,因为我改变了颜色,但有没有办法可以一次抓住所有绿色文本?我试过谷歌搜索但是...它没有真正想出任何好的方法。
有人能指出我正确的方向吗?

Say that string = "Hello World!" Hello color is green World is black. How do I grab Hello out base on its color from the Jtextpane. I've tried the clunky way which is just storing the selected word as I change there color but is there a way where I can grab all the green text in one go? I've tried googling but...it hasn't really came up with any good methods. Can anyone point me in the right direction?

推荐答案

可能有很多方法可以做到这一点,但是。 ..

There's probably a number of ways to do this, but...

你需要获得支持 JTextPane的 StyleDocument 的引用,从给定的字符位置开始,你需要检查给定颜色的字符属性,如果 true ,继续查看文本字符,否则你已经完成了。

You need to get a reference to the StyleDocument that is backing the JTextPane, starting at a given character position, you need to check the characters attributes for the given color, if true, continue onto the text character, else you're done.

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Srap {

    public static void main(String[] args) {
        JTextPane textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();

        Style style = textPane.addStyle("I'm a Style", null);
        StyleConstants.setForeground(style, Color.red);

        try {
            doc.insertString(doc.getLength(), "BLAH ", style);
        } catch (BadLocationException ex) {
        }

        StyleConstants.setForeground(style, Color.blue);

        try {
            doc.insertString(doc.getLength(), "BLEH", style);
        } catch (BadLocationException e) {
        }

        Color color = null;
        int startIndex = 0;
        do {
            Element element = doc.getCharacterElement(startIndex);
            color = doc.getForeground(element.getAttributes());
            startIndex++;
        } while (!color.equals(Color.RED));
        startIndex--;

        if (startIndex >= 0) {

            int endIndex = startIndex;
            do {
                Element element = doc.getCharacterElement(endIndex);
                color = doc.getForeground(element.getAttributes());
                endIndex++;
            } while (color.equals(Color.RED));
            endIndex--;
            if (endIndex > startIndex) {
                try {
                    String text = doc.getText(startIndex, endIndex);
                    System.out.println("Red text = " + text);
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.out.println("Not Found");
            }
        } else {
            System.out.println("Not Found");
        }
    }
}

此示例simple查找第一个工作是红色的,但你可以轻松地走完整个文件,找到你想要的所有单词...

This example simple finds the first work that is colored red, but you could just as easily walk the entire document and find all the words you want...

这篇关于ColorPane - 可以抓住不同角色的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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