FXML:在水平Box(HBox)中放置多个元素

在Hbox内,我确实希望有一个文本作为标题,以及一个带有 submit 按钮的搜索栏。

标题应位于左侧,而搜索 应位于右侧,而提交按钮

我的方式:

<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”。 问题:只有顶级lvl HBox才能给出对齐方式,换句话说,如果HBox中有一个HBox,则只有顶级 HBox会确定元素的位置放置。

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

chenhui651 回答:FXML:在水平Box(HBox)中放置多个元素

您必须添加一个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>

enter image description here

本文链接:https://www.f2er.com/2788694.html

大家都在问