使用 Java 如何為 JTextPane 中的文字設定字型?
使用 Font 類為文字設定字型。我們首先建立 JTextPane 元件 −
JTextPane textPane = new JTextPane();
現在,使用 Font 類 setFont() 方法設定字型 −
Font font = new Font("Serif", Font.ITALIC, 18);
textPane.setFont(font);下面是一個為文字設定字型的示例 −
示例
package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class SwingDemo {
public static void main(String args[]) throws BadLocationException {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
JTextPane textPane = new JTextPane();
textPane.setBackground(Color.blue);
textPane.setBackground(Color.green);
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setItalic(attributeSet, true);
textPane.setCharacterAttributes(attributeSet, true);
textPane.setText("We waited long for ");
Font font = new Font("Serif", Font.ITALIC, 18);
textPane.setFont(font);
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.addStyle("", null);
StyleConstants.setForeground(style, Color.red);
StyleConstants.setBackground(style, Color.white);
doc.insertString(doc.getLength(), "Game of Thrones ", style);
StyleConstants.setForeground(style, Color.yellow);
StyleConstants.setBackground(style, Color.gray);
doc.insertString(doc.getLength(), "Season 8", style);
JScrollPane scrollPane = new JScrollPane(textPane);
container.add(scrollPane, BorderLayout.CENTER);
frame.setSize(550, 300);
frame.setVisible(true);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP