JTextField中的字体颜色不同 [英] Different font color in a JTextField

查看:134
本文介绍了JTextField中的字体颜色不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用2种颜色在 JTextField 或jLabel中显示文本。

How to display a text in a JTextField ot jLabel with 2 colors.

例如:

1 0 0 0 1 1 1 0 1

1 0 0 0 1 1 1 0 1


textField .setForeground(Color.RED,BLUE);

textField.setForeground(Color.RED ,BLUE);

例如定位单个RED

推荐答案


JTextField中的不同字体颜色

Different font color in a JTextField

你使用 JTextField 取代它无法实现使用 JEditorPane JTextPane

You can't achieve it with JTextField instead use JEditorPane or JTextPane.

了解更多关于

Read more about 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";

}

快照:

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

As per your question try this sample code: (change it as per your requirement)

    // 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中的字体颜色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆