Java中带有对齐文本的多行标签/区域/窗格 [英] Multi line label/area/pane in Java with Justified text
问题描述
我有一个有趣的任务.考虑一个长度未知的String content
.我想将content
和Font font
一起显示为已确定文本.我需要知道如果指定width
,这将占用什么height
.我想在屏幕中间显示此文本,但其宽度必须为width
.
I have an interesting task. Consider a String content
with unknown length. I'd like to display content
with a Font font
with Justified text. I need to know what height
this would take up if I specify the width
. I want to display this text in the middle of the screen but it must have a width of width
.
因此,我想做如下事情.
Hence, I would like to do something such as the following.
//create a JTextPane or JTextArea
pane.setText(content);
pane.setFont(font);
//set the width of the pane here
//get the height of the full text block if its justified
pane.setLocation(screenWidth/2 - width/2, screenHeight/2, height/2);
我该怎么做?
推荐答案
通常,对齐方式是相反的.
Normally, justification is done the other way around.
文本具有固定的高度,您可以通过以下方式之一调整文本的宽度:
The text has a fixed height, and you adjust the width of the text in one of the following ways:
- 在单词之间添加空格.更简单,但外观上不那么吸引人.
- 在字母之间添加空格.这称为字距.
如果您调整高度以使宽度正确,则文本中的每一行都将具有不同的高度.
If you adjust the height so that the width is correct, every line in your text will have a different height.
这篇关于Java中带有对齐文本的多行标签/区域/窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!