【发布时间】:2021-04-09 17:07:59
【问题描述】:
我知道有可用的库(例如 jsyntaxpane)可以在 JEditorPane 上应用样式,但在这里我只专注于通过一个极简示例来应用样式。
我想在不使用任何 HTML / CSS 的情况下对某些关键字进行语法着色。
内容只是纯文本。
最初我只是以http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample2.htm 为例,并将 JTextPane 替换为 JEditorPane。
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
JFrame f = new JFrame("Styles Example 2");
// Create the StyleContext, the document and the pane
StyleContext sc = new StyleContext();
final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
JEditorPane pane = new JEditorPane();
pane.setDocument(doc);
// Create and add the main document style
Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setLeftIndent(mainStyle, 16);
StyleConstants.setRightIndent(mainStyle, 16);
StyleConstants.setFirstLineIndent(mainStyle, 16);
StyleConstants.setFontFamily(mainStyle, "serif");
StyleConstants.setFontSize(mainStyle, 12);
// Create and add the constant width style
final Style cwStyle = sc.addStyle("ConstantWidth", null);
StyleConstants.setFontFamily(cwStyle, "monospaced");
StyleConstants.setForeground(cwStyle, Color.green);
// Create and add the heading style
final Style heading2Style = sc.addStyle("Heading2", null);
StyleConstants.setForeground(heading2Style, Color.red);
StyleConstants.setFontSize(heading2Style, 16);
StyleConstants.setFontFamily(heading2Style, "serif");
StyleConstants.setBold(heading2Style, true);
StyleConstants.setLeftIndent(heading2Style, 8);
StyleConstants.setFirstLineIndent(heading2Style, 0);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
// Set the logical style
doc.setLogicalStyle(0, mainStyle);
// Add the text to the document
doc.insertString(0, text, null);
// Apply the character attributes
doc.setCharacterAttributes(49, 13, cwStyle, false);
doc.setCharacterAttributes(223, 14, cwStyle, false);
doc.setCharacterAttributes(249, 14, cwStyle, false);
doc.setCharacterAttributes(286, 8, cwStyle, false);
doc.setCharacterAttributes(475, 14, cwStyle, false);
doc.setCharacterAttributes(497, 21, cwStyle, false);
doc.setCharacterAttributes(557, 9, cwStyle, false);
doc.setCharacterAttributes(639, 12, cwStyle, false);
doc.setCharacterAttributes(733, 21, cwStyle, false);
doc.setCharacterAttributes(759, 9, cwStyle, false);
// Finally, apply the style to the heading
doc.setParagraphAttributes(0, 1, heading2Style, false);
} catch (BadLocationException e) {
}
}
});
} catch (Exception e) {
System.out.println("Exception when constructing document: " + e);
System.exit(1);
}
f.getContentPane().add(new JScrollPane(pane));
f.setSize(400, 300);
f.setVisible(true);
未应用任何样式,文本以黑色前景和白色背景显示。
受这个问题的答案启发,我也有一个较短的版本:
Java Swing JEditorPane: manipulating styled documents
JFrame f = new JFrame("Styles Example 2");
StyleContext sc = new StyleContext();
final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
JEditorPane pane = new JEditorPane();
pane.setDocument(doc);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
try {
doc.insertString(doc.getLength(), "\nSome more text", keyWord);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(pane));
f.setSize(400, 300);
f.setVisible(true);
我在这里错过了什么?
【问题讨论】: