【问题标题】:Different font color in a JTextFieldJTextField 中的不同字体颜色
【发布时间】:2014-06-08 09:52:53
【问题描述】:

如何在JTextField ot jLabel 中以两种颜色显示文本。

例如:

1 0 0 0 1 1 1 0 1

textField.setForeground(Color.RED ,BLUE);

例如定位单个 RED

【问题讨论】:

    标签: java swing jtextfield


    【解决方案1】:

    JTextField 中不同的字体颜色

    您无法使用JTextField 来实现它,而是使用JEditorPaneJTextPane

    阅读更多关于How to Use Editor Panes and Text Panes


    使用JTextPane 的示例代码直接来自HERE

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    
    public class StylesExample12 {
        public static void main(String[] args) {
            JFrame f = new JFrame("Styles Example 1");
    
            // Create the StyleContext, the document and the pane
            StyleContext sc = new StyleContext();
            final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
            JTextPane pane = new JTextPane(doc);
    
            // Create and add the style
            final Style heading2Style = sc.addStyle("Heading2", null);
            heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
            heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
            heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
            heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));
    
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        try {
                            // Add the text to the document
                            doc.insertString(0, text, null);
    
                            // Finally, apply the style to the heading
                            doc.setParagraphAttributes(0, 1, heading2Style, false);
                        } catch (BadLocationException e) {
                        }
                    }
                });
            } catch (Exception e) {
                System.out.println("Exception when constructing document: " + e);
                System.exit(1);
            }
    
            f.getContentPane().add(new JScrollPane(pane));
            f.setSize(400, 300);
            f.setVisible(true);
        }
    
        public static final String text = "Attributes, Styles and Style Contexts\n"
                + "The simple PlainDocument class that you saw in the previous "
                + "chapter is only capable of holding text. The more complex text "
                + "components use a more sophisticated model that implements the "
                + "StyledDocument interface. StyledDocument is a sub-interface of "
                + "Document that contains methods for manipulating attributes that "
                + "control the way in which the text in the document is displayed. "
                + "The Swing text package contains a concrete implementation of "
                + "StyledDocument called DefaultStyledDocument that is used as the "
                + "default model for JTextPane and is also the base class from which "
                + "more specific models, such as the HTMLDocument class that handles "
                + "input in HTML format, can be created. In order to make use of "
                + "DefaultStyledDocument and JTextPane, you need to understand how "
                + "Swing represents and uses attributes.\n";
    
    }
    

    快照:


    编辑

    根据您的问题尝试以下示例代码:(根据您的要求更改)

        // Create and add the style
        final Style redStyle = sc.addStyle("RED", null);
        redStyle.addAttribute(StyleConstants.Foreground, Color.red);
        redStyle.addAttribute(StyleConstants.FontSize, new Integer(16));
    
        final Style blueStyle = sc.addStyle("BLUE", null);
        blueStyle.addAttribute(StyleConstants.Foreground, Color.blue);
        blueStyle.addAttribute(StyleConstants.FontSize, new Integer(14));
        blueStyle.addAttribute(StyleConstants.Bold, new Boolean(true));
    
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        String[] text = { "1a", "0b", "0c", "0d", "1e", "1f", "1g", "0h", "1i" };
                        for (int i = 0; i < text.length; i++) {
                            String s = text[i];
    
                            // Finally, apply the style to the heading
                            int start = pane.getText().length();
                            Style style = null;
                            if (i % 2 == 0) {
                                style = redStyle;
                            } else {
                                style = blueStyle;
                            }
                            // Add the text to the document
                            doc.insertString(start, s + " ", style);
                        }
                    } catch (BadLocationException e) {
                    }
                }
            });
        } catch (Exception e) {
            System.out.println("Exception when constructing document: " + e);
            System.exit(1);
        }
    

    快照:

    【讨论】:

    • 好吧,如果你在JTextField 中使用StyledDocument ,你可能会使用技术;)
    • @MadProgrammer 我之前也有人问过同样的问题。我们可以用JTextField 为每个单词使用不同的颜色吗? :)
    • @Braj 好的。在 jTextPane 中如何做到这一点!
    • 我测试了JTextField,它不想工作,可惜,这本来是一个不错的解决方案:P
    • @MadProgrammer 我认为JTextField 风格适用于整个文本。
    猜你喜欢
    • 2015-02-22
    • 2012-06-23
    • 2019-04-21
    • 2010-10-13
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多