简体   繁体   English

GridPane中的JavaFX居中VBox

[英]JavaFX Centering VBox inside GridPane

would really appreciate some help with my problem. 非常感谢您为我的问题提供的帮助。

I'm using JavaFX and the NetBeans IDE. 我正在使用JavaFX和NetBeans IDE。 I am making a very simple launch window for a desktop client. 我正在为桌面客户端制作一个非常简单的启动窗口。 The window currently looks like this image. 该窗口当前看起来像此图像。

Current Look: 当前外观:

当前外观

Wanted Look (editted with paint): 想要的外观(用油漆编辑):

想要的样子

In other words, I want the buttons to be: 换句话说,我希望按钮为:

  1. centered underneath the 'Welcome' text 在“欢迎”文字下方居中
  2. maintain their current width 保持其当前宽度

My current setup is: 我当前的设置是:

I have a GridPane acting as the root. 我有一个GridPane作为根。 Not sure why JavaFX chose to use (col,row), but that's how I'll represent my setup in the following: 不知道为什么JavaFX选择使用(col,row),但这就是我在下面表示设置的方式:

  • @GridPane(0,0) -> 'Welcome' Text @GridPane(0,0)->'欢迎'文本
  • @GridPane(0,1) -> VBox (this VBox contains the 2 buttons shown in above image) @GridPane(0,1)-> VBox(此VBox包含上图中显示的2个按钮)

The relevant code (or what I think is relevant, please correct me if I am wrong) is below: 相关代码(或我认为相关的代码,如果我错了,请纠正我)如下:

//adds GridPane to Scene
GridPane grid = new GridPane();
RowConstraints row1 = new RowConstraints();
row1.setPercentHeight(25);
grid.getRowConstraints().add(row1);
grid.setAlignment(Pos.CENTER);
Scene scene = new Scene(grid, 400, 300,Color.WHITESMOKE);

//Create welcome message and add it to grid, and align it to the center
Text sceneTitle = new Text("Welcome");
grid.add(sceneTitle,0,0);
GridPane.setHalignment(sceneTitle,HPos.CENTER);

//Create buttons and fix their widths
Button newBtn = new Button("Create New Project");
Button loadBtn = new Button("Load Existing Project");
newBtn.setMaxWidth(150);
loadBtn.setMaxWidth(150);

//Create a VBox, add children to it, and then add it to the grid
VBox vBtns = new VBox();
vBtns.setSpacing(5);
vBtns.getChildren().addAll(newBtn,loadBtn);
grid.add(vBtns,0,1);

I've tried a lot of different things.. to mention some of the more 'logical' ones: 我尝试了很多不同的方法..提到一些更“合乎逻辑的”方法:

//1. Center the VBox within its current GridPane cell
GridPane.setHalignment(vBtns,HPos.CENTER);

//2. Center the buttons within the VBox 
newBtn.setAlignment(Pos.CENTER);
loadBtn.setAlignment(Pos.CENTER);

I've never been good at GUI programming. 我从来都不擅长GUI编程。 It's very discouraging when something that should be so simple takes so long to figure out. 当应该这么简单的事情花很长时间才能弄清楚时,这是非常令人沮丧的。 Are there any developers out there who can help me find a solution? 有没有可以帮助我找到解决方案的开发人员?

To fix it with your current setup, you just need: 要使用当前设置修复它,您只需要:

vBtns.setAlignment(Pos.CENTER);

However, this just seems overly complex. 但是,这似乎过于复杂。 Why not just do 为什么不做

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LayoutTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        RowConstraints row1 = new RowConstraints();
        row1.setPercentHeight(25);
        grid.getRowConstraints().add(row1);

        ColumnConstraints colConstraints = new ColumnConstraints();
        colConstraints.setHalignment(HPos.CENTER);
        grid.getColumnConstraints().add(colConstraints);

        grid.setAlignment(Pos.CENTER);
        Scene scene = new Scene(grid, 400, 300,Color.WHITESMOKE);

        //Create welcome message and add it to grid, and align it to the center
        Text sceneTitle = new Text("Welcome");
        sceneTitle.setStyle("-fx-font-size:48;");
        grid.add(sceneTitle,0,0);

        //Create buttons and fix their widths
        Button newBtn = new Button("Create New Project");
        Button loadBtn = new Button("Load Existing Project");
        newBtn.setMaxWidth(150);
        loadBtn.setMaxWidth(150);

        grid.add(newBtn, 0, 1);

        GridPane.setMargin(loadBtn, new Insets(5, 0, 5, 0));
        grid.add(loadBtn, 0, 2);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM