JavaFX的HBox中对齐 [英] JavaFX HBox Alignment
问题描述
我一直在JavaFX的一个软件,我有一个愚蠢的,但令人担忧的问题。
I've been working on a software in javaFX and I have a stupid but worrying problem.
在的code我有一个的HBox
某一部分,而且,这三个项目里面:一个图片
,一个标签
和垂直框
。
In certain part of the code I have a HBox
, and, inside of it three items: an image
, a label
and a VBox
.
问题是,我想有图片
左对齐,也就是旁边的左边距窗口
和垂直框
相一致的权利,也就是旁边的窗口
,我不知道该怎么做。
The issue is that I would like to have the image
aligned to the left, that is, next to the left margin of the window
, and the VBox
aligned to the right, that is, next to the right border of the window
and I don't know how to do it.
我试图使用 VBox.setAlignment(Pos.RIGHT_CENTER)
,但没有奏效。
I've tried to use VBox.setAlignment(Pos.RIGHT_CENTER)
, but it didn't work.
推荐答案
这是一个最常见的排列问题,当您要放置对布局的两个角的一个项目。
This is a most common alignment issue when you want to place an item towards the two corners of the Layout.
让我们说,你想有:
HBox
|
ImageView (Left)
Label (Center)
VBox (Right)
我很简单的解决方法是使用两个额外的地区
。一个在ImageView的和放大器之间;标签。另外在标签和垂直框之间。
I very simple solution is to use two extra Regions
. One in between ImageView & Label. The other in between Label and VBox.
HBox
|
ImageView (Left)
Region
Label (Center)
Region
VBox (Right)
这些地区必须具备 HGrow
设置为 Priority.Always
,因此,如果您调整HBox中,这两个将增长,保持其他元素原封不动中的位置。
These Regions must have HGrow
set as Priority.Always
, so that if you resize the HBox, these two will grow, keeping the other elements intact in their location.
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
在这两个地区。
Please note the HBox.hgrow="ALWAYS"
in both the Regions.
输出
这篇关于JavaFX的HBox中对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!