亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創建沒有中心的 JavaFX BorderPane?

如何創建沒有中心的 JavaFX BorderPane?

胡子哥哥 2022-03-10 10:33:30
我想在 JavaFX 中創建一個沒有中心窗格的 BorderPane 布局。到目前為止我寫的代碼只實現了左右邊框,如下:import javafx.application.Application;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class GUI_Practice extends Application {    @Override    public void start(Stage stage) throws Exception {        String blackBorder = "-fx-border-style: solid; -fx-border-width: 1; -fx-border-color: black";        /* Left column */        Button save = new Button("Save");        Button del = new Button("Delete");        HBox settings = new HBox(save, del);        VBox leftCol = new VBox(settings);        leftCol.setStyle(blackBorder);        /* Right column */        Button calculate = new Button("Calculate");        Button cancel = new Button("Cancel");        HBox runButtons = new HBox(calculate, cancel);        VBox rightCol = new VBox(runButtons);        rightCol.setStyle(blackBorder);        /* Set up borderpane */        BorderPane root = new BorderPane();        root.setPadding(new Insets(15));        root.setLeft(leftCol);        root.setRight(rightCol);        Scene scene = new Scene(root, 800, 600);        stage.setScene(scene);        stage.show();    }    public static void main(String[] args) {        launch();    }}它給出的輸出如下圖所示:但是,我希望它看起來更像這樣:其中左右列的寬度相等,并占據了窗口的整個寬度。此外,列不隨窗口改變寬度,所以中間的空白會隨著窗口變大而變大。我需要更改什么才能使列填充窗口的寬度?(PS我還在學習,所以如果解決方案可以避免FXML(我還不明白),那就太好了)
查看完整描述

1 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

有不同的方式來得到這個問題的解決。

  1. 如果你想獲得還是從利益BorderPane(比如有頂部和底部窗格),您可以設置HBox/GridPane為中心(無設置左/右)。

  2. 如果你沒有打擾約頂部和底部布局的實現,那么作為@ K88建議,您可以直接使用HBoxGridPane作為根節點。

使用HBox

HBox.setHGrow(leftCol,Priority.ALWAYS);

HBox.setHGrow(rightCol,Priority.ALWAYS);

HBox root = new HBox();

root.setPadding(new Insets(15));

root.getChildren().addAll(leftCol, rightCol);

使用GridPane:


GridPane root = new GridPane();

ColumnConstraints col1 = new ColumnConstraints();

col1.setPercentWidth(50);

ColumnConstraints col2 = new ColumnConstraints();

col2.setPercentWidth(50);

root.getColumnConstraints().addAll(col1,col2);

root.addRow(0, leftCol,rightCol);

更新:在這兩種情況下,如果你希望你的按鈕,自動拉伸,綁定的按鈕其布局的寬度。這樣,您就可以控制按鈕的寬度的比例HBox。


Button calculate = new Button("Calculate");

Button cancel = new Button("Cancel");

HBox runButtons = new HBox(calculate, cancel);

calculate.prefWidthProperty().bind(runButtons.widthProperty().divide(2));

cancel.prefWidthProperty().bind(runButtons.widthProperty().divide(2));

更新2:請看看下面的樣本演示。


import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.*;

import javafx.stage.Stage;


public class Sample extends Application {


    public void start(Stage stage) throws Exception {

        String blackBorder = "-fx-border-style: solid; -fx-border-width: 1; -fx-border-color: black";


        Button calculate = new Button("Calculate");

        Button cancel = new Button("Cancel");

        HBox runButtons = new HBox(calculate, cancel);

        calculate.prefWidthProperty().bind(runButtons.widthProperty().divide(2));

        cancel.prefWidthProperty().bind(runButtons.widthProperty().divide(2));

        VBox rightCol = new VBox(runButtons);

        rightCol.setStyle(blackBorder);


        Button save = new Button("Save");

        Button del = new Button("Delete");

        HBox settings = new HBox(save, del);

        save.prefWidthProperty().bind(settings.widthProperty().divide(3)); // 1/3

        del.prefWidthProperty().bind(settings.widthProperty().divide(3).multiply(2)); // 2/3

        VBox leftCol = new VBox(settings);

        leftCol.setStyle(blackBorder);



        GridPane root = new GridPane();

        ColumnConstraints col1 = new ColumnConstraints();

        col1.setPercentWidth(50);

        ColumnConstraints col2 = new ColumnConstraints();

        col2.setPercentWidth(50);

        root.getColumnConstraints().addAll(col1,col2);

        root.addRow(0, leftCol,rightCol);


        Scene scene = new Scene(root, 800, 600);

        stage.setScene(scene);

        stage.show();

    }


    public static void main(String... a) {

        Application.launch(a);

    }

}


查看完整回答
反對 回復 2022-03-10
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號