我一直在使用 JavaFX 开发一个软件,但我遇到了一个愚蠢但令人担忧的问题。
在代码的某些部分我有一个HBox
,并且,其中包含三项:image
, a label
and a VBox
.
问题是我想要image
向左对齐,即紧挨着左边距window
,以及VBox
向右对齐,即靠近右边框window
我不知道该怎么做。
我尝试过使用VBox.setAlignment(Pos.RIGHT_CENTER)
,但没有成功。
当您想要将项目放置在布局的两个角时,这是最常见的对齐问题。
假设您想要:
HBox
|
ImageView (Left)
Label (Center)
VBox (Right)
我非常简单的解决方案是使用两个额外的Regions
。介于 ImageView 和 Label 之间的一种。另一个介于 Label 和 VBox 之间。
HBox
|
ImageView (Left)
Region
Label (Center)
Region
VBox (Right)
这些地区必须有HGrow
set as 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>
请注意HBox.hgrow="ALWAYS"
在这两个地区。
Output
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)