【问题标题】:Center objects in HBox and VBox (JavaFx)HBox 和 VBox (JavaFx) 中的中心对象
【发布时间】:2021-09-24 05:09:05
【问题描述】:

我正在为我的一项任务制作一个简单的 GUI。我创建了一个裸骨接口,目前遇到了一些问题。该程序显示一切都很好,只是我想将对象居中以使其看起来更整洁。我尝试将按钮(属于底部)放到一个borderPane上,并使用setCetner方法将它与中心对齐,但什么也没做。还有其他方法可以尝试将我的所有对象集中在窗格上吗?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main extends Application {

    private TextField startDate;
    private Text start;
    private Text end;
    private TextField endDate;
    private Button count;

    @Override
    public void start(Stage primaryStage) {

        start = new Text("Start Date: ");
        end = new Text("End Date: ");

        startDate = new TextField("1/1/2000");
        endDate = new TextField(getDate());

        count = new Button("Count");

        HBox startLine = new HBox(10);
        startLine.getChildren().addAll(start, startDate);


        HBox endLine = new HBox(10);
        endLine.getChildren().addAll(end, endDate);

        HBox button = new HBox();
        button.getChildren().add(count);
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(button);


        VBox vbox = new VBox(10);
        vbox.getChildren().addAll(startLine, endLine, button);


        Pane root = new Pane();

        root.getChildren().addAll(vbox);

        Scene scene = new Scene(root, 400, 300);
        primaryStage.setTitle("Date Counter");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public String getDate(){
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }

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

}

【问题讨论】:

  • 为什么你不使用 Scenebuilder ?因此您可以轻松更改位置。
  • 阅读(至少!)不同布局的 api 文档并应用你学到的东西 :)
  • hbox.setAlignment(Pos.BASELINE_CENTER);

标签: java user-interface javafx methods layout


【解决方案1】:

为了让 UI 看起来更好看,我更喜欢 GridPane(例如,Textfields 是对齐的)

// set the "coordinates" of the nodes
GridPane.setConstraints(start, 0, 0);
GridPane.setConstraints(startDate, 1, 0);
GridPane.setConstraints(end, 0, 1);
GridPane.setConstraints(endDate, 1, 1);
// center button here
GridPane.setConstraints(count, 0, 2, 2, 1, HPos.CENTER, VPos.CENTER);

// some spacing, otherwise the nodes stick together
Insets spacing = new Insets(3d);
GridPane.setMargin(start, spacing);
GridPane.setMargin(startDate, spacing);
GridPane.setMargin(end, spacing);
GridPane.setMargin(endDate, spacing);
GridPane.setMargin(count, spacing);

// nodes to the grid
GridPane grid = new GridPane();
grid.getChildren().addAll(start, startDate, end, endDate, count);

Scene scene = new Scene(grid, 400, 300);

如果要使整个网格居中,则必须添加 grid.setAlignment(Pos.TOP_CENTER);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2016-09-03
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多