java - JTextArea更改下一个追加的背景颜色

标签 java fonts jtextarea

我有一个用户不可编辑的 JTextArea。 它充当控制台但没有输入。我只想更改下一个附加的背景颜色,但不知道如何更改。我有一个想法:

  1. 创建 Font 类型的实例并以某种方式设置此字体对象的背景颜色
  2. 在调用下一个追加之前调用方法 JTextArea.setFont(我之前创建的实例)。
  3. call JTextArea.append("带有背景颜色的消息\n");

我认为它有效,但我不知道如何设置字体对象的 BackGroundColor 属性。有人能给我一些见解吗?谢谢。

最佳答案

您不能使用 JTextArea。它不支持不同的字体颜色。

相反,您需要使用 JTextPane 并且可以使用属性。这是一个帮助您入门的简单示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextPaneAttributes extends JPanel
{

    public TextPaneAttributes()
    {
        setLayout( new BorderLayout() );

        JTextPane textPane = new JTextPane();
        textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );

//      DefaultHighlighter highlighter =  (DefaultHighlighter)textPane.getHighlighter();
//      highlighter.setDrawsLayeredHighlights(false);

        //  Define some character and paragraph attributes

        SimpleAttributeSet keyWord = new SimpleAttributeSet();
        StyleConstants.setBold(keyWord, true);

        SimpleAttributeSet green = new SimpleAttributeSet();
        StyleConstants.setForeground(green, Color.GREEN);

        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

        SimpleAttributeSet left = new SimpleAttributeSet();
        StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);

        //  Change attributes on some existing text

        StyledDocument doc = textPane.getStyledDocument();
        doc.setCharacterAttributes(0, 3, keyWord, false);
        doc.setCharacterAttributes(8, 5, green, true);
        doc.setParagraphAttributes(20, 1 , center, false);

        //  Add some text with attributes

        try
        {
            doc.insertString(doc.getLength(), "\nNormal text", null);
            doc.insertString(doc.getLength(), "\nGreen text centered", green);
            doc.setParagraphAttributes(doc.getLength(), 1 , center, false);
            doc.insertString(doc.getLength(), "\nKeyword text", keyWord);
            doc.setParagraphAttributes(doc.getLength(), 1 , left, false);

            //  Newly typed text at the end of the document will inherit the
            //  "keyword" attributes unless we remove the attributes

            textPane.setCaretPosition(doc.getLength());
            textPane.getInputAttributes().removeAttributes(keyWord);
        }
        catch(Exception e) {}

        //  Add text pane to frame

        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setPreferredSize( new Dimension( 200, 250 ) );
        add( scrollPane );

        //  Create a Button panel

        JPanel buttons = new JPanel();
        add(buttons, BorderLayout.PAGE_END);

        //  Add a Bold button

        JButton bold = new JButton( new StyledEditorKit.BoldAction() );
        buttons.add( bold );

        //  Add Right Alignment button

        JButton right = new JButton( new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT) );
        buttons.add( right );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextPaneAttributes());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

我将让您阅读 StyleConstants API,了解您可以控制的其他属性,包括文本的背景颜色。您可以为每个属性集设置多个属性。

阅读 Swing 教程中关于 Text Component Features 的部分了解更多信息和工作示例。

关于java - JTextArea更改下一个追加的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43240946/

相关文章:

java - Hibernate:Ejb3Configuration 类的替代品

java - 将文本文件中的矩阵存储到二维数组中

java - 安卓开发 : MediaPlayer not working after "hold mode" is on/off

.Net 不一致的字体渲染

linux - 将字体嵌入到 PDF 表单中

java - TextAreaSample JTextArea JScrollPane

java - 如何使用 Thread.sleep() 和 setBackground() 在 Swing 中创建闪光效果?

java - 如何将 Windows Azure 存储 blob 发送到浏览器并更改 header

css - CSS : font: 0px/0px a; 中这是什么声明

java - JTextArea 中是否可以具有非可变尺寸?