簡體   English   中英

HBox 和 VBox (JavaFx) 中的中心對象

[英]Center objects in HBox and VBox (JavaFx)

我正在為我的一項任務制作一個簡單的 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);
    }

}

為了讓 UI 看起來更好,我更喜歡GridPane (例如Text字段對齊)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM