簡體   English   中英

在JTextArea或JTextPane中居中文本 - 水平文本對齊

[英]Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment

有沒有辦法為JTextArea創建水平居中的文本,就像使用JTextField一樣?

setHorizontalAlignment(JTextField.CENTER);

有沒有辦法可以用多行文本區域完成同樣的事情? 我用JTextArea找不到它的方法,那么還有其他選擇嗎? 的JTextPane? 如果是這樣,怎么樣?

您需要使用JTextPane並使用屬性。 以下內容應以所有文本為中心:

StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);

編輯:

據我所知,不支持垂直居中。 以下是一些您可能會覺得有用的代碼: JTextPane的垂直對齊方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM