首页 文章

JavaFX 8 - 定位HBox的文本垂直中心

提问于
浏览
13

我很难将文本放在HBox中 . 我能够设置文本的水平对齐,但我无法设置文本的垂直对齐 . 我试图在Hbox的垂直中心设置文本,但它出现在顶部 .

任何帮助将非常感激 .

// Create The Updates Progress Bar At The Bottom Of The Window
HBox checkUpdatesBar = new HBox();
checkUpdatesBar.setId("checkUpdatesBar");
checkUpdatesBar.setPrefSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMinSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);
checkUpdatesBar.setMaxSize(CHECK_PANE_WIDTH, CHECK_PANE_HEIGHT);

// Create The 'Check For Updates...' Text
Text checkForUpdates = new Text();
checkForUpdates.setFont(Font.font("Arial Black", FontWeight.BLACK, 15));
checkForUpdates.setWrappingWidth(CHECK_PANE_WIDTH / 2.8);
checkForUpdates.setFill(Color.WHITE);
checkForUpdates.setTextAlignment(TextAlignment.CENTER);
checkForUpdates.setText("Checking For Updates ...".toUpperCase());

checkUpdatesBar.getChildren().add(checkForUpdates);

我的代码生成以下内容:http://puu.sh/ccEkp/41a26038a4.png

我喜欢我的水平定位 . 我知道它不在中间,因为我将它设置为那样 . 但是,我只关心 Vertical Positioning .

我分别尝试了以下内容:

checkForUpdates.setLayoutY(20);
checkForUpdates.setY(20);

我选择数字20是因为我想将文本从HBox顶部向下移动20个像素 . 我在Photoshop中计算了这个值 . 虽然如果有更好的方法将文本设置为HBox的垂直中心,请赐教 .

再次感谢您的帮助!

2 回答

  • 31

    如果您只想将文本放在 HBox 的中心,可以使用 HBoxalignment property 来执行此操作 .

    将对齐设置为 CENTER_LEFT ,以放置子项,从HBox垂直空间的CENTER开始

    checkUpdatesBar.setAlignment(Pos.CENTER_LEFT);
    
  • 4

    如果您使用的是SceneBuilder,请选择您的HBox,然后在“检查器”部分中,更改

    属性:节点:对齐

    值为CENTER_LEFT:

    enter image description here

相关问题