Rumah >Java >javaTutorial >Bagaimana untuk Mencapai Pewarna Teks Tersuai dalam Java Swing Beyond JTextArea?

Bagaimana untuk Mencapai Pewarna Teks Tersuai dalam Java Swing Beyond JTextArea?

Susan Sarandon
Susan Sarandonasal
2024-11-27 00:46:10505semak imbas

How to Achieve Custom Text Coloring in Java Swing Beyond JTextArea?

Mewarna Teks Tersuai dalam Alternatif JTextArea

Dalam JTextArea, penggunaan pemformatan teks mempengaruhi keseluruhan dokumen dan bukannya aksara atau bahagian tertentu. Untuk mencapai pewarnaan teks tersuai, pertimbangkan untuk menggunakan komponen alternatif seperti JTextPane atau JEditorPane.

Mewarna Teks Tersuai Menggunakan JTextPane

JTextPane membolehkan anda menetapkan warna yang berbeza untuk bahagian teks tertentu :

import java.awt.Color;
import java.awt.Insets;
import java.awt.event.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextColoringDemo {

    public void setTextColors() {
        JTextPane textPane = new JTextPane();
        textPane.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

        StyleContext styleContext = StyleContext.getDefaultStyleContext();
        AttributeSet blueAttributeSet = styleContext.addAttribute(
                SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground,
                Color.BLUE
        );

        AttributeSet greenAttributeSet = styleContext.addAttribute(
                SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground,
                Color.GREEN
        );

        AttributeSet redAttributeSet = styleContext.addAttribute(
                SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground,
                Color.RED
        );

        AttributeSet orangeAttributeSet = styleContext.addAttribute(
                SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground,
                Color.ORANGE
        );

        // Set the color of specific text sections
        textPane.setCharacterAttributes(blueAttributeSet, true);
        textPane.replaceSelection("LOAD R1, 1\n");
        textPane.setCharacterAttributes(blueAttributeSet, true);
        textPane.replaceSelection("DEC R1\n");
        textPane.setCharacterAttributes(blueAttributeSet, true);
        textPane.replaceSelection("STORE M, R1\n");
        textPane.setCharacterAttributes(blueAttributeSet, true);
        textPane.replaceSelection("ADD R4, R1,8\n");

        textPane.setCharacterAttributes(greenAttributeSet, true);
        textPane.replaceSelection("R1\n");
        textPane.setCharacterAttributes(greenAttributeSet, true);
        textPane.replaceSelection("R4\n");

        textPane.setCharacterAttributes(redAttributeSet, true);
        textPane.replaceSelection("M\n");

        textPane.setCharacterAttributes(orangeAttributeSet, true);
        textPane.replaceSelection("1\n");
        textPane.setCharacterAttributes(orangeAttributeSet, true);
        textPane.replaceSelection("8\n");
    }
}

Pendekatan ini membolehkan anda menyesuaikan warna mana-mana bahagian teks yang ditentukan dalam komponen JTextPane.

Atas ialah kandungan terperinci Bagaimana untuk Mencapai Pewarna Teks Tersuai dalam Java Swing Beyond JTextArea?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn