[英]Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment
Is there a way to create horizontally centered text for a JTextArea like with a JTextField? 有没有办法为JTextArea创建水平居中的文本,就像使用JTextField一样?
setHorizontalAlignment(JTextField.CENTER);
Is there a way I can accomplish the same thing with a multi-line text area? 有没有办法可以用多行文本区域完成同样的事情? I can't find a method for it with JTextArea, so is there another option? 我用JTextArea找不到它的方法,那么还有其他选择吗? JTextPane? 的JTextPane? If so, how? 如果是这样,怎么样?
You need to use a JTextPane and use attributes. 您需要使用JTextPane并使用属性。 The following should center all the text: 以下内容应以所有文本为中心:
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
Edit: 编辑:
Vertical centering is not supported as far as I know. 据我所知,不支持垂直居中。 Here is some code you might find useful: Vertical Alignment of JTextPane 以下是一些您可能会觉得有用的代码: JTextPane的垂直对齐方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.