JavaFX HBox 对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29707882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:28:55  来源:igfitidea点击:

JavaFX HBox Alignment

javajavafxalignmentvboxhbox

提问by JOSEMAFUEN

I've been working on a software using JavaFX and I have a stupid but worrying problem.

我一直在使用 JavaFX 开发一个软件,但我遇到了一个愚蠢但令人担忧的问题。

In certain part of the code I have a HBox, and, inside of it three items: an image, a labeland a VBox.

在代码的某些部分,我有一个HBox, 并且在其中包含三个项目: an image、 alabel和 a VBox

The issue is that I would like to have the imagealigned to the left, that is, next to the left margin of the window, and the VBoxaligned to the right, that is, next to the right border of the windowand I don't know how to do it.

问题是我想让image左对齐,即在 的左边距旁边window,而VBox右对齐,即在 的右边框旁边window,我不知道如何去做吧。

I've tried to use VBox.setAlignment(Pos.RIGHT_CENTER), but it didn't work.

我试过使用VBox.setAlignment(Pos.RIGHT_CENTER),但没有用。

回答by kcpr

I think that the best option could be switching from HBoxto BorderPane. It let You make items sticked to any edge of Your window.
Another option is GridPane. You can select column and change its 'Halignment' property to 'RIGHT'.

我认为最好的选择可能是从 切换HBoxBorderPane. 它让您可以将物品粘在窗户的任何边缘。
另一种选择是GridPane。您可以选择列并将其“Halignment”属性更改为“RIGHT”。

And, by the way, I recommend using JavaFX Scene Builderwhile having fun with JavaFX.

而且,顺便说一下,我建议在享受 JavaFX 乐趣的同时使用JavaFX Scene Builder

回答by ItachiUchiha

This is a most common alignment issue when you want to place an item towards the two corners of the Layout.

当您想将项目放置在布局的两个角上时,这是最常见的对齐问题。

Let us say you want to have :

让我们说你想要:

HBox
  |
  ImageView (Left)
  Label (Center)
  VBox (Right)

I very simple solution is to use two extra Regions. One in between ImageView & Label. The other in between Label and VBox.

我很简单的解决办法是使用两个额外的Regions. 一种介于 ImageView 和 Label 之间。另一个介于 Label 和 VBox 之间。

HBox
  |
  ImageView (Left)
  Region
  Label (Center)
  Region
  VBox (Right)

These Regions must have HGrowset as Priority.Always, so that if you resize the HBox, these two will grow, keeping the other elements intact in their location.

这些区域必须HGrow设置为Priority.Always,以便如果您调整 HBox 的大小,这两个区域将增长,同时保持其他元素在它们的位置不变。

FXML example:

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>

Please note the HBox.hgrow="ALWAYS"in both the Regions.

请注意HBox.hgrow="ALWAYS"两个区域中的 。

Output

输出

enter image description here

在此处输入图片说明