Java – how to set each character to a different color / background color in jtextpane?

I have been looking for this period of time. So far, what I have been able to think of is how to create a style and apply it to the following roles:

StyledDocument doc = (StyledDocument) new DefaultStyledDocument();
JTextPane textpane = new JTextPane(doc);
textpane.setText("Test");
javax.swing.text.Style style = textpane.addStyle("Red",null);
StyleConstants.setForeground(style,Color.RED);
doc.setCharacterAttributes(0,1,textpane.getStyle("Red"),true);

This is useful if you have only a few styles in your document and want to store them by name so that they can be easily applied In my application, I want to set the foreground color (only one of a few values) and background color (grayscale, many different values) independently for each character in the text To create hundreds of potential / different styles seems a huge waste Is there a way to set these properties without having to create a new style at a time? It would be easier if I just needed to render the text, but I also needed to make it editable Is there any way to use jtextpane to do this, or does another placement class provide this function?

Solution

If you want to change the style of each character in the text box, here is a complete random way You create a different property set for each character It's up to you to find the right combination (foreground / background contrast, different character sizes, etc.) You can also store different styles that have been applied so that the same style is not used twice

import java.awt.Color;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class TestDifferentStyles {
    private void initUI() {
        JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

        StyledDocument doc = new DefaultStyledDocument();
        JTextPane textPane = new JTextPane(doc);
        textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has "
                + "been the industry's standard dummy text ever since the 1500s,when an unkNown printer took a galley of "
                + "type and scrambled it to make a type specimen book. It has survived not only five centuries,but also the "
                + "leap into electronic typesetting,remaining essentially unchanged. It was popularised in the 1960s with the"
                + " release of Letraset sheets containing Lorem Ipsum passages,and more recently with desktop publishing "
                + "software like Aldus PageMaker including versions of Lorem Ipsum.");

        Random random = new Random();
        for (int i = 0; i < textPane.getDocument().getLength(); i++) {
            SimpleAttributeSet set = new SimpleAttributeSet();
            // StyleConstants.setBackground(set,new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
            StyleConstants.setForeground(set,random.nextInt(256)));
            StyleConstants.setFontSize(set,random.nextInt(12) + 12);
            StyleConstants.setBold(set,random.nextBoolean());
            StyleConstants.setItalic(set,random.nextBoolean());
            StyleConstants.setUnderline(set,random.nextBoolean());

            doc.setCharacterAttributes(i,set,true);
        }

        frame.add(new JScrollPane(textPane));
        frame.setSize(500,400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokelater(new Runnable() {
            @Override
            public void run() {
                new TestDifferentStyles().initUI();
            }
        });
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>