📅  最后修改于: 2020-10-01 03:23:27             🧑  作者: Mango
JTextPane是JEditorPane类的子类。 JTextPane用于带有嵌入式图像和组件的样式化文档。它是文本组件,可以用图形表示的属性进行标记。 JTextPane使用DefaultStyledDocument作为其默认模型。
Constructor | Description |
---|---|
JTextPane() | It creates a new JTextPane. |
JtextPane(StyledDocument doc) | It creates a new JTextPane, with a specified document model. |
Modifier and Type | Method | Description |
---|---|---|
Style | addStyle(String nm, Style parent) | It adds a new style into the logical style hierarchy. |
AttributeSet | getCharacterAttributes() | It fetches the character attributes in effect at the current location of the caret, or null. |
StyledDocument | getStyledDocument() | It fetches the model associated with the editor. |
void | setDocument(Document doc) | It associates the editor with a text document. |
void | setCharacterAttributes(AttributeSet attr, boolean replace) | It applies the given attributes to character content. |
void | removeStyle(String nm) | It removes a named non-null style previously added to the document. |
void | setEditorKit(EditorKit kit) | It sets the currently installed kit for handling content. |
void | setStyledDocument(StyledDocument doc) | It associates the editor with a text document. |
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class JTextPaneExample {
public static void main(String args[]) throws BadLocationException {
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
JTextPane pane = new JTextPane();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
// Set the attributes before adding text
pane.setCharacterAttributes(attributeSet, true);
pane.setText("Welcome");
attributeSet = new SimpleAttributeSet();
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
StyleConstants.setBackground(attributeSet, Color.blue);
Document doc = pane.getStyledDocument();
doc.insertString(doc.getLength(), "To Java ", attributeSet);
attributeSet = new SimpleAttributeSet();
doc.insertString(doc.getLength(), "World", attributeSet);
JScrollPane scrollPane = new JScrollPane(pane);
cp.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
输出量