📄 textpaneattributes.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class TextPaneAttributes extends JFrame {
public TextPaneAttributes() {
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
// Set alignment to be centered for all paragraphs
MutableAttributeSet standard = new SimpleAttributeSet();
StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, 0, standard, true);
// Define a keyword attribute
HTML.Tag []tags=HTML.getAllTags();
HTML.Attribute[] atrs=HTML.getAllAttributeKeys();
MutableAttributeSet keyWord = new SimpleAttributeSet();
MutableAttributeSet tagWord = new SimpleAttributeSet();
keyWord.addAttribute(tags[0].toString().toLowerCase(),tags[0].toString());
StyleConstants.setForeground(keyWord, Color.red);
StyleConstants.setForeground(tagWord, Color.BLUE);
StyleConstants.setItalic(keyWord, true);
StyleConstants.setBold(tagWord,true);
// Add initial text
textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nbody\nbgcolor\n");
// Highlight some keywords
doc.setCharacterAttributes(0, 3, keyWord, false);
doc.setCharacterAttributes(19, 4, keyWord, false);
// Add some text
try {
doc.insertString(0, "Start of text\n", null);
doc.insertString(doc.getLength(), "End of text\n", keyWord);
} catch (Exception e) {
}
// Add text pane to frame
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
getContentPane().add(scrollPane);
// Add a bold button
JButton button = new JButton("bold");
getContentPane().add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StyledEditorKit.BoldAction().actionPerformed(null);
}
});
}
public static void main(String[] args) {
TextPaneAttributes frame = new TextPaneAttributes();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -