Ita*_*iha 37
当您想要将项目放置在布局的两个角上时,这是最常见的对齐问题.
让我们说你想拥有:
HBox
|
ImageView (Left)
Label (Center)
VBox (Right)
Run Code Online (Sandbox Code Playgroud)
我非常简单的解决方案是使用两个额外的Regions.一个介于ImageView和Label之间.另一个在Label和VBox之间.
HBox
|
ImageView (Left)
Region
Label (Center)
Region
VBox (Right)
Run Code Online (Sandbox Code Playgroud)
这些区域必须HGrow设置为Priority.Always,因此如果您调整HBox的大小,这两个区域将会增长,保持其他元素在其位置保持不变.
FXML示例:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="150.0" fitWidth="140.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="http://www.imaginaformacion.com/wp-content/uploads/2010/06/JavaFx.png" />
</image>
</ImageView>
<Region prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
<Label prefHeight="17.0" prefWidth="205.0" text="Label On the Center" />
<Region prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
<VBox alignment="CENTER_RIGHT" prefHeight="94.0" prefWidth="200.0">
<children>
<Label prefHeight="17.0" prefWidth="200.0" text="Label Inside the VBox" />
</children>
</VBox>
</children>
</HBox>
Run Code Online (Sandbox Code Playgroud)
请注意HBox.hgrow="ALWAYS"两个地区.
产量

我认为最好的选择可能是切换HBox到BorderPane.它允许您将物品粘贴到窗口的任何边缘.
另一种选择是GridPane.您可以选择列并将其"Halignment"属性更改为"RIGHT".
顺便说一句,我建议使用JavaFX Scene Builder,同时享受JavaFX的乐趣.
| 归档时间: |
|
| 查看次数: |
34972 次 |
| 最近记录: |