FXML:在水平 Box(HBox) 中定位多个元素

FXML: positioning multiple elements within an horizontal Box(HBox)

在 Hbox 中,我确实想要一个文本作为标题,以及一个带有 提交 按钮的搜索栏。

title 应该放在左边但是 search bar提交右边的按钮。

我的做法:

<HBox>  
    <Label text="Penfactory Software"/> 
    <HBox alignment="TOP_RIGHT">
        <TextField fx:id="idSearch"  />
        <Button fx:id="idSubmit" text ="Submit" onAction="#submit"/> 
    </HBox>
</HBox>

Hbox可以给它的元素一个对齐="TOP_RIGHT"的位置。 问题:只有 Top lvl HBox 可以给出对齐方式,换句话说,如果 HBox 中有一个 HBox 只有 top-level HBox 会 determine 放置元素的位置。

如何实现上述目标,即标题在左侧,搜索 + 按钮在右侧?

您必须添加一个 Pane。将 Pane's 最大宽度设置为 MAX_VALUE,将 Hgrow 设置为 ALWAYS

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label text="Hello world!">
         <font>
            <Font size="17.0" />
         </font>
      </Label>
      <Pane maxHeight="-Infinity" maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS" />
      <TextField />
      <Button mnemonicParsing="false" text="Button" />
   </children>
</HBox>