【问题标题】:JTextArea change next append's background colorJTextArea 更改下一个追加的背景颜色
【发布时间】:2017-08-31 15:27:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: java fonts jtextarea


    【解决方案1】:

    您不能使用 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,了解您可以控制的其他属性,包括文本的背景颜色。您可以为每个属性集设置多个属性。

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

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2019-08-18
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2013-03-03
      • 2020-09-02
      • 2013-08-08
      相关资源
      最近更新 更多