Java JTextArea更改下一个追加';背景色

Java JTextArea更改下一个追加';背景色,java,fonts,jtextarea,Java,Fonts,Jtextarea,我有一个JTextArea,它不是用户可编辑的。 它充当控制台,但没有输入。我只想更改下一个附加的背景色,但不知道如何更改。我有一个想法: 创建字体类型的实例,并以某种方式设置此字体对象的背景色 在调用下一个append之前调用方法JTextArea.setFont(我之前创建的实例) 调用JTextArea.append(“带有背景色的消息\n”) 我认为它可以工作,但我不知道如何设置字体对象的BackGroundColor属性。有人能给我一些见解吗?谢谢。您不能使用JTextArea。它不支

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

  • 创建字体类型的实例,并以某种方式设置此字体对象的背景色
  • 在调用下一个append之前调用方法JTextArea.setFont(我之前创建的实例)
  • 调用JTextArea.append(“带有背景色的消息\n”)
    我认为它可以工作,但我不知道如何设置字体对象的BackGroundColor属性。有人能给我一些见解吗?谢谢。

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

    相反,你需要使用
    JTextPane
    ,你可以玩attibutes。下面是一个简单的示例,让您开始学习:

    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教程的部分