我想向我的 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/