java 如何使 JavaFX 文本换行工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40631744/
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-11-03 05:21:37  来源:igfitidea点击:

How to make JavaFX text wrap work?

javajavafxword-wrap

提问by Radu Stefan Popescu

I can't for the life of me make JavaFX wrap text. Instead my boxes get the size of the Text.

我一辈子都不能让 JavaFX 换行。相反,我的盒子得到了Text.

This is my main:

这是我的主要内容:

BorderPane main = FXMLLoader.load(getClass().getResource("main.fxml"));

primaryStage.setTitle("D394");
primaryStage.setScene(new Scene(main, 1000, 500));
primaryStage.getScene().getStylesheets().add("main.css");
primaryStage.show();

And this is my main.fxml. It's pretty big, sorry for that.

这是我的 main.fxml。它很大,对不起。

<BorderPane prefHeight="401.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.112-ea"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="ro.softvisionmedia.gui.MainController">
    <top>
        <MenuBar fx:id="menuBar" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity">
            <Menu items="" text="Lala">
            </Menu>
        </MenuBar>
    </top>

    <center>
        <TabPane fx:id="tabPane">

            <Tab id="tab_1" fx:id="tab1_D394" closable="false" text="D394">
            </Tab>

            <Tab fx:id="tab2_DateIdentif" closable="false" text="Date identificare">
                <TabPane fx:id="tabPane_dateIdentif">
                    <Tab fx:id="identifPersTVA" closable="false" text="Persoana inregistrata in scopuri de TVA">
                    </Tab>

                    <Tab fx:id="identifRepFiscal" closable="false" text="Reprezentant fiscal/legal/imputernicit">
                    </Tab>
                    <Tab fx:id="identifIntocmit" closable="false" text="Intocmitor">
                        <VBox fx:id="vBoxIntocmitor">
                            <HBox>
                                <TextFlow>
                                    <Text text="Subsemnatul "/>
                                    <Text fx:id="denumireRtext"/>
                                    <Text text=", in calitate de reprezentant legal, "/>
                                    <TextField fx:id="functieReprez"/>
                                    <Text>declar, sub
                                        sanctiunile aplicate faptei de fals in acte publice, ca datele din aceasta
                                        declaratie sunt corecte si complete, iar persoana care a intocmit prezenta
                                        declaratie este
                                    </Text>
                                </TextFlow>
                            </HBox>
                            <HBox>
                                <Label text="Denumire/nume prenume"/>
                                <TextField fx:id="denumireIntocmitor"/>
                                <Label text="CUI/CNP/NIF"/>
                                <TextField fx:id="cifIntocmitor"/>
                            </HBox>
                            <HBox>
                                <Label text="Persoana juridica"/>
                                <RadioButton fx:id="tipIntocmitorPJ"/>
                                <Label text="Calitate"/>
                                <TextField fx:id="calitateIntocmitor"/>
                            </HBox>
                            <HBox>
                                <Label text="Persoana fizica"/>
                                <RadioButton fx:id="tipIntocmitorPF"/>
                                <Label text="Functia in cadrul persoanei impozabile"/>
                                <TextField fx:id="functieIntocmitor"/>
                                <Label text="Alta calitate"/>
                                <Text text="?????"/>
                                <!-- @todo ce vine aici? -->
                            </HBox>
                            <HBox>
                                <TextFlow>
                                    <Text text="De asemenea, subsemnatul "/>
                                    <Text fx:id="denumireRtext2"/>
                                    <Text text=",in calitate de reprezentant legal "/>
                                    <Text fx:id="functieReprezText"/>
                                    <Text text=" declar:"/>
                                </TextFlow>
                            </HBox>
                            <HBox>
                                <Text>1. sunt de acord ca, pentru anul fiscal 2016, in sensul prevederilor art. 11 alin
                                    (3) lit. d) din Legea nr. 207/2015 privind Codul de procedura fiscala, datele
                                    inscrise in
                                    prezenta declaratie referitoare la tranzactiile derulate cu fiecare persoana
                                    impozabila (client/furnizor) inregistrata in scopuri de TVA sa fie consultate de
                                    catre aceasta
                                    prin intermediul aplicatiei informatice pusa la dispozitie de ANAF.
                                </Text>
                                <RadioButton fx:id="optinueDA" text="Da"/>
                                <RadioButton fx:id="optiuneNU" text="Nu"/>
                            </HBox>
                            <HBox>
                                <Text>2. Sunt de acord cu schimbarea optiunii , astfel ca pentru anul fiscal 2016, in
                                    sensul prevederilor art. 11 alin (3) lit. d) din Legea nr. 207/2015 privind Codul de
                                    procedura fiscala, datele inscrise in prezenta declaratie referitoare la
                                    tranzactiile derulate cu fiecare persoana impozabila (client/furnizor) inregistrata
                                    in scopuri de
                                    TVA sa fie consultate de catre aceasta prin intermediul aplicatiei informatice pusa
                                    la dispozitie de ANAF
                                </Text>
                                <RadioButton fx:id="schimbOptiuneDA" text="Da"/>
                                <RadioButton fx:id="schimbOptiuneNU" text="Nu"/>
                            </HBox>
                        </VBox>
                    </Tab>
                </TabPane>
            </Tab>
        </TabPane>
    </center>

    <bottom>
        <HBox prefHeight="0.0" prefWidth="897.0">
            <Button onAction="#nextTab" text="Pagina urmatoare"/>
            <Button onAction="#validate" text="Valideaza XML-ul"/>
            <Button onAction="#validateCreate" text="Valideaza XML-ul si creeaza PDF"/>
            <Button onAction="#validateCreateSign" text="Valideaza XML-ul si creeaza PDF semnat"/>
        </HBox>
    </bottom>
</BorderPane>

This is how it ends up lookingsetPrefWidthof the text does nothing

这就是它最终查看setPrefWidth文本的方式

Making the HBoxes smaller does make the text wrap, but it ends up on top of text from the other HBoxes.

使HBoxes 变小确实会使文本换行,但它最终位于其他HBoxes的文本之上。

What I want is for my text to wrap to the width of the HBoxes which should be at the same size as the parent actual size.

我想要的是让我的文本环绕HBoxes的宽度,该宽度应该与父实际大小相同。

My controller doesn't do anything in terms of styling and layout, and my .css doesn't do anything.

我的控制器在样式和布局方面没有做任何事情,而我的 .css 没有做任何事情。

I'm using java8.

我正在使用java8。

回答by DVarga

You can use the wrappingWidthPropertyto define the wrapping width in pixels.

您可以使用wrappingWidthProperty来定义以像素为单位的环绕宽度。

You can use it in a binding:

您可以在绑定中使用它:

textID.wrappingWidthProperty().bind(tabPane.widthProperty());

This binds the mentioned property to the width of the TabPane. Note: Binding to the HBoxor the VBoxwill not work, as their width is the actual width of the Text(the HBoxis resized to the width of the Textand the VBoxis resized to the width of the HBox).

这将上述属性绑定到TabPane. 注:绑定到HBoxVBox将无法正常工作,因为它们的宽度的实际宽度Text(将HBox被调整至的宽度TextVBox被调整至的宽度HBox)。

回答by Jim Broiles

I have had this similar need in a simple dialog box. The root of the dialog box is a 2x2 GridPane. Each row contains an HBox that spans both columns. I use a Label to display a message in the HBox in row 0. To wrap the text I use the following: Label label = new Label(message); label.setWrapText(true);

我在一个简单的对话框中也有过类似的需求。对话框的根是一个 2x2 GridPane。每行包含一个跨越两列的 HBox。我使用标签在第 0 行的 HBox 中显示一条消息。要包装文本,我使用以下内容: Label label = new Label(message); label.setWrapText(true);

Then, of course, you add the label to the HBox. For your example, perhaps all you need to do is change Text to Label and this will work.

然后,当然,您将标签添加到 HBox。对于您的示例,也许您需要做的就是将 Text 更改为 Label ,这将起作用。

If you are using SceneBuilder you can go into the properties tab of the Label and click the WrapText checkbox.

如果您使用的是 SceneBuilder,您可以进入标签的属性选项卡并单击 WrapText 复选框。