簡體   English   中英

將階段分為2個網格窗格JavaFX

[英]Divide stage into 2 gridpanes JavaFX

因此,我試圖在左側顯示文本,在右側顯示按鈕,文本應具有恆定的大小,並且按鈕應調整大小以填充窗口的其余部分。

到目前為止,這是我的結果:

至今...

我不想在按鈕上輸入文字,而是希望它們共享整個窗口。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application {

    GridPane buttons = new GridPane();
    GridPane textGrid = new GridPane();
    @Override
    public void start(Stage primaryStage) {

       StackPane root = new StackPane();
       Button button1 = new Button();
       Button button2 = new Button();
       Button button3 = new Button();
       Button button4 = new Button();
       Button button5 = new Button();

       button1.setText("Button1");
       button2.setText("Button4");
       button3.setText("Button3");
       button4.setText("Button4");
       button5.setText("Button5");


       TextArea text1 = new TextArea();
       text1.setText("Test");
       text1.setPrefSize(100, 100);

       button1.prefWidthProperty().bind(buttons.widthProperty());
       button2.prefWidthProperty().bind(buttons.widthProperty());
       button3.prefWidthProperty().bind(buttons.widthProperty());
       button4.prefWidthProperty().bind(buttons.widthProperty());
       button5.prefWidthProperty().bind(buttons.widthProperty());

       button1.prefHeightProperty().bind(buttons.heightProperty());
       button2.prefHeightProperty().bind(buttons.heightProperty());
       button3.prefHeightProperty().bind(buttons.heightProperty());
       button4.prefHeightProperty().bind(buttons.heightProperty());
       button5.prefHeightProperty().bind(buttons.heightProperty());


       buttons.addColumn(0, button1, button2, button3, button4, button5);

       textGrid.addColumn(0, text1);


        Scene scene = new Scene(root, 280, 180);

        root.getChildren().addAll(buttons, textGrid);

        buttons.setAlignment(Pos.TOP_RIGHT);
        textGrid.setAlignment(Pos.TOP_LEFT); 

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

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

}

通常,讓布局窗格處理布局管理比嘗試通過綁定管理布局更好。

這是一個示例:

布局

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.util.stream.IntStream;

public class Main extends Application {

    private static final int N_BUTTONS = 5;

    @Override
    public void start(Stage stage) {
        VBox buttonLayout = new VBox(
                10,
                IntStream.range(0, N_BUTTONS)
                        .mapToObj(this::createButton)
                        .toArray(Button[]::new)
        );
        HBox.setHgrow(buttonLayout, Priority.ALWAYS);

        TextArea textArea = new TextArea("Test");
        textArea.setPrefWidth(100);
        textArea.setMaxWidth(TextArea.USE_PREF_SIZE);
        textArea.setMinWidth(TextArea.USE_PREF_SIZE);

        HBox layout = new HBox(10, textArea, buttonLayout);
        layout.setPadding(new Insets(10));

        Scene scene = new Scene(layout);

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

    private Button createButton(int i) {
        Button button = new Button("Button " + i);
//        button.setMaxWidth(Double.MAX_VALUE);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        VBox.setVgrow(button, Priority.ALWAYS);

        return button;
    }

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

根據示例,我將指出以下幾點:

  1. 由於按鈕是如此相似,因此請在循環中創建按鈕,而不是在代碼中單獨創建按鈕。 我將IntStream范圍與map和toArray ,但是您可以使用標准的for循環(可能更容易理解)執行相同的操作。
  2. 使用標准布局窗格的組合來實現您的布局。 例如,按鈕垂直間隔,因此將它們放在VBox中,文本和按鈕彼此水平,因此使用HBox。
  3. 在布局上使用約束條件,以約束它們執行所需的布局,例如HBox.setHgrow(buttonLayout, Priority.ALWAYS); 告訴Box始終將Box中的所有額外空間分配給buttonLayout,以便按鈕將填充所有剩余區域。
  4. 在各個節點上設置約束以根據需要調整大小,例如,以下代碼為textArea建立了固定寬度,該寬度不會改變(如果需要,您可以使用類似的代碼來建立固定高度):

     textArea.setPrefWidth(100); textArea.setMaxWidth(TextArea.USE_PREF_SIZE); textArea.setMinWidth(TextArea.USE_PREF_SIZE); 
  5. 某些控件會自動將自身擴展到其最大大小以外,默認情況下不會啟用按鈕,請使用以下代碼來啟用此行為(如果您只想擴展寬度而不是高度,則只能設置maxWidth而不是maxSize) :

     button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
  6. 與其像本例中那樣在代碼中定義布局,不如使用SceneBuilder之類的工具直觀地創建場景並將布局保存為FXML文件 ,以使布局與代碼分離(類似地,將任何樣式都放置在外部CSS中文件)。

暫無
暫無

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

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