Java FX 文本区域对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20381993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:32:08  来源:igfitidea点击:

Java FX TextArea Alignment

javatextareajavafxjavafx-2

提问by user2410265

I want to display a text inside a Java FX Text area.

我想在 Java FX 文本区域内显示文本。

My code is:

我的代码是:

TextArea txt = new TextArea();
txt.setEditable(false);
txt.setStyle("-fx-font-alignment: center");
txt.setText(text);

But I'm not able to set a text alignment (inside the TextArea)

但我无法设置文本对齐方式(在 内TextArea

I tried this:

我试过这个:

txt.setStyle("-fx-font-alignment: center");

But it didn't work

但它没有用

回答by WonderWorld

I don't know why you want to use a textArea, but if you just want to display some text you should use a TextField.

我不知道你为什么要使用 textArea,但如果你只想显示一些文本,你应该使用 TextField。

Or else to display text use:

否则显示文本使用:

Text text = new Text("text");

But i don't know what exactly it is that you want to do.

但我不知道你到底想做什么。

回答by Martynas

Apply the style to the text child:

将样式应用于文本子项:

.text-area *.text {
    -fx-text-alignment: center;
}

回答by richa_v

Try setStyle(-fx-padding: 0 10 0 10).

尝试 setStyle(-fx-padding: 0 10 0 10)。

so in this case, 0 - top 10 - right 0 - bottom 10 - left padding.

所以在这种情况下,0 - 顶部 10 - 右侧 0 - 底部 10 - 左侧填充。

You can replace 10 with your required value. It simply fits your text in the text box with a padding/space of 10. Had a similar problem. This thing worked for me.

您可以用所需的值替换 10。它只是适合文本框中的文本,填充/空格为 10。有类似的问题。这件事对我有用。

回答by Naman Gaur

You can use Scene Builder instead for making the UI, it is super easy. There you can easily use "Java fx text area" and everything. all you have to do is to import the jfoenix library and you are ready,and that too without coding.

您可以使用 Scene Builder 来制作 UI,这非常简单。在那里您可以轻松使用“Java FX 文本区域”和所有内容。你所要做的就是导入 jfoenix 库,你就准备好了,而且不需要编码。

回答by siouxes

I found a solution for centering horizontally the content of textarea only using CSS.

我找到了一个仅使用 CSS 将 textarea 的内容水平居中的解决方案。

i have used a class in css:

我在 css 中使用了一个类:

.centeredTextArea .scroll-pane .content .text {
    -fx-text-alignment: center;
}

and after i have added

在我添加之后

styleClass="centeredTextArea"

to my textArea control.

到我的 textArea 控件。