Java 添加全局 JTextPane 样式/属性?

标签 java swing styles jtextpane

我想向我的 JTextPane 添加一个全局 AttributeSet。

我发现了这个:

SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);

来自 http://java-sl.com/tip_hanging_first_line.html

我想知道如何设置“默认样式表”? (不使用 HTML)。然后我尝试了这个:

StyleContext style = new StyleContext();
Style s = style.addStyle("test", null);
StyleConstants.setForeground(s, Color.BLUE);
StyledDocument d = (StyledDocument) console.getOutputField().getDocument();

来自 http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm没有运气。

我知道 StyledDocument 具有用于设置前景色等内容的特定属性 - 这就是为什么这可能不起作用 - 但任何人都可以指出如何使用其他样式属性吗?比如左缩进和首行缩进。

最佳答案

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
StyleConstants.setForeground(style, Color.BLUE);
doc.setParagraphAttributes(0, doc.getLength(), style, true);

关于Java 添加全局 JTextPane 样式/属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389180/

相关文章:

java - 从 URL 读取 90 MB 的巨大文件

java - 将图像添加到 JLabel,并在 GridLayout 中显示标签

java - JTabbedPane 及其选项卡透明度不起作用

java - 使用 JFrame 和 JPanel 构建 GUI 时如何使用布局管理器?

html - 确保页脚始终位于底部的最佳方法是什么

css - 为什么 SASS 没有局部变量?

java - 实现自己的 jar 时出现 NoClassDefFoundError

java - 使用正则表达式匹配分隔列表中的子字符串

java - 从对象获取 JavaArray 属性

php - 如何在wordpress站点中为一页加载样式