gpt4 book ai didi

JavaFX 将 VBox 置于 GridPane 内居中

转载 作者:行者123 更新时间:2023-11-30 07:37:42 27 4
gpt4 key购买 nike

非常感谢您对我的问题提供帮助。

我正在使用 JavaFX 和 NetBeans IDE。我正在为桌面客户端制作一个非常简单的启动窗口。该窗口当前看起来像这个图像。

当前外观:

current look

想要的外观(用油漆编辑):

wanted look

换句话说,我希望按钮是:

  1. 位于“欢迎”文字下方居中
  2. 保持当前宽度

我当前的设置是:

我有一个 GridPane 作为根。不确定为什么 JavaFX 选择使用 (col,row),但这就是我在下面表示我的设置的方式:

  • @GridPane(0,0) -> “欢迎”文本
  • @GridPane(0,1) -> VBox(此 VBox 包含上图中显示的 2 个按钮)

相关代码(或者我认为相关的,如果我错了请纠正我)如下:

//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);

我尝试了很多不同的事情..提一些更“合乎逻辑”的事情:

//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);

我从来不擅长 GUI 编程。当一些本应如此简单的事情需要这么长时间才能弄清楚时,这是非常令人沮丧的。有开发人员可以帮助我找到解决方案吗?

最佳答案

要使用当前设置修复此问题,您只需:

vBtns.setAlignment(Pos.CENTER);

但是,这似乎过于复杂。为什么不直接做

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);
}
}

关于JavaFX 将 VBox 置于 GridPane 内居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35159841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com