我一直在使用JavaFX开发一个软件,我有一个愚蠢但令人担忧的问题。
在代码的某些部分,我有一个HBox,在它里面有三个项目:一个image,一个label和一个VBox。
问题是我希望image靠左对齐,即紧靠window的左边距,而VBox靠右对齐,即紧靠window的右边界,我不知道该怎么做。
我试过使用VBox.setAlignment(Pos.RIGHT_CENTER),但它不起作用。
发布于 2015-06-14 15:21:33
当您想要将项目朝向布局的两个角放置时,这是最常见的对齐问题。
假设你想要:
HBox
|
ImageView (Left)
Label (Center)
VBox (Right)我非常简单的解决方案是使用两个额外的Regions。介于ImageView和Label之间的一个。另一个在Label和VBox之间。
HBox
|
ImageView (Left)
Region
Label (Center)
Region
VBox (Right)这些区域必须将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>请注意这两个区域的HBox.hgrow="ALWAYS"。
输出

发布于 2015-06-14 04:04:29
我认为最好的选择是从HBox切换到BorderPane。它允许您将项目粘贴到窗口任意边缘。
另一个选择是GridPane。您可以选择列并将其“column”属性更改为“RIGHT”。
顺便说一句,我建议在享受JavaFX乐趣的同时使用JavaFX Scene Builder。
https://stackoverflow.com/questions/29707882
复制相似问题