java textfield 颜色_java – JTextField中的不同字体颜色

这篇博客介绍了如何在Java Swing应用中使用JTextPane显示不同颜色的文本。通过创建不同的样式,并设置前景色、字体大小和样式,可以为特定文本段落应用红色或蓝色高亮。示例代码展示了如何动态插入和应用这些样式,使得文本在JTextPane中以交替颜色显示。
摘要由CSDN通过智能技术生成

如何在JTextField或两种颜色的jLabel中显示文本.

例如:

1 0 0 0 1 1 1 0 1

textField.setForeground(Color.RED ,BLUE);

例如,定位单个RED

最佳答案

Different font color in a JTextField

直接从HERE使用JTextPane的示例代码

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);

}

快照:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值