首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Swing JTextPane 字體顏色與樣式完整設置指南

     編輯:Java 相關知識

核心代碼示例

完整可運行的 Demo,展示如何通過 StyledDocument 與 StyleConstants 設置字體、顏色、大小、粗斜體、下劃線、對齊等屬性。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.*;

public class NewJFrame extends JFrame implements ActionListener {
    private JPanel jp1;
    private JButton color, font;
    private JTextPane jep;
    private JScrollPane jsp;

    public static void main(String[] args) {
        new NewJFrame().setVisible(true);
    }

    public NewJFrame() { initGUI(); }

    private void initGUI() {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        // 工具欄
        jp1 = new JPanel();
        add(jp1, BorderLayout.NORTH);
        font = new JButton("font"); font.addActionListener(this); jp1.add(font);
        color = new JButton("color"); color.addActionListener(this); jp1.add(color);
        // 編輯區
        jsp = new JScrollPane(); add(jsp, BorderLayout.CENTER);
        jep = new JTextPane(); jep.setDocument(new DefaultStyledDocument());
        jsp.setViewportView(jep);
        pack(); setSize(400, 300);
    }

    // 通用字符屬性設置
    public static void setCharacterAttributes(JEditorPane editor, AttributeSet attr, boolean replace) {
        int p0 = editor.getSelectionStart(), p1 = editor.getSelectionEnd();
        if (p0 != p1) {
            StyledDocument doc = getStyledDocument(editor);
            doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
        }
        StyledEditorKit kit = getStyledEditorKit(editor);
        MutableAttributeSet inputAttr = kit.getInputAttributes();
        if (replace) inputAttr.removeAttributes(inputAttr);
        inputAttr.addAttributes(attr);
    }
    // 通用段落屬性設置
    public static void setParagraphAttributes(JEditorPane editor, AttributeSet attr, boolean replace) {
        int p0 = editor.getSelectionStart(), p1 = editor.getSelectionEnd();
        StyledDocument doc = getStyledDocument(editor);
        doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
    }
    protected static StyledDocument getStyledDocument(JEditorPane e) {
        Document d = e.getDocument();
        if (d instanceof StyledDocument) return (StyledDocument) d;
        throw new IllegalArgumentException("document must be StyledDocument");
    }
    protected static StyledEditorKit getStyledEditorKit(JEditorPane e) {
        EditorKit k = e.getEditorKit();
        if (k instanceof StyledEditorKit) return (StyledEditorKit) k;
        throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
    }

    // 具體屬性設置方法
    public static void setFontSize(JEditorPane editor, int size) {
        if (editor != null && size > 0 && size < 512) {
            MutableAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setFontSize(attr, size);
            setCharacterAttributes(editor, attr, false);
        }
    }
    public static void setForeground(JEditorPane editor, Color fg) {
        if (editor != null && fg != null) {
            MutableAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setForeground(attr, fg);
            setCharacterAttributes(editor, attr, false);
        }
    }
    public static void setBackground(JEditorPane editor, Color bg) {
        if (editor != null && bg != null) {
            MutableAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setBackground(attr, bg);
            setCharacterAttributes(editor, attr, false);
        }
    }
    public static void setFontFamily(JEditorPane editor, String family) {
        if (editor != null) {
            MutableAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setFontFamily(attr, family);
            setCharacterAttributes(editor, attr, false);
        }
    }
    public static void setBold(JEditorPane editor, boolean bold) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setBold(attr, bold);
        setCharacterAttributes(editor, attr, false);
    }
    public static void setItalic(JEditorPane editor, boolean italic) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setItalic(attr, italic);
        setCharacterAttributes(editor, attr, false);
    }
    public static void setUnderline(JEditorPane editor, boolean underline) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setUnderline(attr, underline);
        setCharacterAttributes(editor, attr, false);
    }
    public static void setAlignment(JEditorPane editor, int align) { // 0:left,1:center,2:right
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setAlignment(attr, align);
        setParagraphAttributes(editor, attr, false);
    }

    // 事件處理:點擊按鈕設置字體大小與前景色
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == font) setFontSize(jep, 20);
        if (e.getSource() == color) setForeground(jep, Color.red);
    }
}

常用屬性設置速查表

功能關鍵代碼
字體家族StyleConstants.setFontFamily(attr, family)
字體大小StyleConstants.setFontSize(attr, size)
粗體StyleConstants.setBold(attr, true/false)
斜體StyleConstants.setItalic(attr, true/false)
下劃線StyleConstants.setUnderline(attr, true/false)
前景色StyleConstants.setForeground(attr, Color)
背景色StyleConstants.setBackground(attr, Color)
對齊StyleConstants.setAlignment(attr, 0/1/2) + setParagraphAttributes

所有字符屬性調用 setCharacterAttributes,段落屬性調用 setParagraphAttributes。選區為空時設置輸入屬性(新輸入文本生效),有選區則修改選區現有文本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved