设置字体颜色

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import java.awt.*;

/**
 * Created by ai on 17/8/18.
 */
public class TextPaneTest extends JFrame {

    private JPanel topPanel;
    private JTextPane tPane;

    public TextPaneTest() {
        topPanel = new JPanel();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        tPane = new JTextPane();
        tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        tPane.setMargin(new Insets(5, 5, 5, 5));

        topPanel.add(tPane);

        appendToPane(tPane, "这里是红色\n", Color.RED);
        appendToPane(tPane, "蓝色的字 ", Color.BLUE);
        appendToPane(tPane, "我猜", Color.DARK_GRAY);
        appendToPane(tPane, "我再猜", Color.MAGENTA);
        appendToPane(tPane, "我猜猜猜", Color.ORANGE);

        add(topPanel);
        pack();
        setVisible(true);
    }

    private void appendToPane(JTextPane tp, String msg, Color c) {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "宋体");
        aset = sc.addAttribute(aset, StyleConstants.Alignment,
                StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    public static void main(String[] args) {
        new TextPaneTest();
    }
}

  

时间: 2024-08-25 20:20:44

设置字体颜色的相关文章

android 设置字体颜色、EditText自动输入转换成大写字母的多种方式

在TextView上面设置某一个字的字体颜色为指定颜色时,可以通过java类SpannableString类和Html语言来实现. (一)SpannableString类方式 private void setText(TextView t){ String text = t.getText().toString().trim(); SpannableString span = new SpannableString(text); span.setSpan(new ForegroundColorS

gedit embeded terminal 设置字体 颜色

# -*- coding: utf8 -*- # terminal.py - Embeded VTE terminal for gedit # This file is part of gedit # # Copyright (C) 2005-2006 - Paolo Borelli # # gedit is free software; you can redistribute it and/or modify # it under the terms of the GNU General P

MarkDown设置字体颜色

记录一下如何设置字体颜色和大小 <font color=red size=5>gray</font> <font color="#4590a3" size="6px">文字</font> 效果如下: red 文字 原文地址:https://www.cnblogs.com/tenny-peng/p/11532668.html

TextView设置字体颜色

TextView textView = null; // 第一种:利用系统自带的颜色进行设置,如: textView.setTextColor(android.graphics.Color.BLUE); // 第二种:0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示,如: textView.setTextColor(0xf

python设置字体颜色

在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信息. 我们可以通过对有用的信息设置不同颜色来达到醒目的效果,因为我平时都是在linux下开发,而linux终端中的颜色是用转义序列控制的,转义序列是以ESC开头,可以用\033完成相同的工作(ESC的ASCII码用十进制表示就是27,等于用八进制表示的33). 书写格式,和相关说明如下: 复制代码代

Android动态设置字体颜色

步骤: 1.在values文件夹下的strings.xml文件里添加颜色:比如 <color name="ccc">#ccc</color> 2.如果你直接这样写: tv.setTextColor(R.color.ccc); 理论上是不行的,不过我发现有时行,有时不行. 正确的写法应该是: tv.setTextColor(this.getResources().getColor(R.color.ccc)); ok! 所以在java代码里用到项目资源的话,不要忘记

Windows DOS 窗口设置字体颜色

2015-04-21 10:54:43 1 #include <windows.h> 2 #include <iostream> 3 using namespace std; 4 #define Color_count 15 5 6 //void SetColor(unsigned short ForeColor = 3, unsigned short BackgroundColor = 0) 7 void SetColor(int color) 8 { 9 if(color &l

请教下关于CKEditor富文本编辑框设置字体颜色的问题

CKEDITOR.editorConfig = function( config ){ config.plugins = 'about,a11yhelp,basicstyles,bidi,blockquote,clipboard,colorbutton,colordialog,contextmenu,dialogadvtab,div,elementspath,enterkey,entities,filebrowser,find,flash,floatingspace,font,format,fo

print 函数设置字体颜色

格式:\033[显示方式;前景色;背景色m数值表示的参数含义:显示方式: 0(默认值).1(高亮).22(非粗体).4(下划线).24(非下划线). 5(闪烁).25(非闪烁).7(反显).27(非反显)前景色: 30(黑色).31(红色).32(绿色). 33(黄色).34(蓝色).35(洋 红).36(青色).37(白色)背景色: 40(黑色).41(红色).42(绿色). 43(黄色).44(蓝色).45(洋 红).46(青色).47(白色) print('\033[0;31;1m nih