zoukankan      html  css  js  c++  java
  • Java设置字体颜色

    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, "这里是红色
    ", 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();
        }
    }
    

      

  • 相关阅读:
    1.Mybatis的全局配置mybatis-config.xml
    01淘淘商城项目:项目Maven工程搭建
    Connection timed out: connect; Communications link failure
    启动maven项目的配置
    PLSQL 触发器概念
    Git 概念&常用命令
    Git与svn的区别 & Git的工作流程
    Redis 是如何存储的
    Redis 概念,常用命令
    idea 快捷键
  • 原文地址:https://www.cnblogs.com/dreammyone/p/7388864.html
Copyright © 2011-2022 走看看