簡體   English   中英

JavaFx中的“窗格”是什么?

[英]What is “pane” in JavaFx?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ButtonInPane extends Application {
@Override 
// Override the start method in the Application class

public void start(Stage primaryStage) {
// Create a scene and place a button in the scene

StackPane pane = new StackPane();
pane.getChildren().add(new Button("OK"));
Scene scene = new Scene(pane, 200, 50);
primaryStage.setTitle("Button in a pane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

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

上面的代碼創建一個帶有按鈕的圖形用戶界面。 但是我不知道什么是窗格以及為什么需要它。 我也不明白為什么在添加按鈕時需要調用getChildren方法,例如

"pane.getChildren().add(new Button("OK"));".

布局手冊中

JavaFX應用程序可以通過設置每個UI元素的position和size屬性來手動布置UI。 但是,一個更簡單的選擇是使用布局窗格。 JavaFX SDK提供了幾個布局窗格,用於輕松設置和管理經典布局,例如行,列,堆棧,圖塊等。 調整窗口大小時,布局窗格會根據節點的屬性自動調整其位置和大小。

javaFX中有6個面板,例如:BorderPane,StackPane,GridPane,FlowPane,TilePane和AnchorPane。

StackPane

堆棧窗格使您可以將許多節點一個接一個地放置。

StackPane root = new StackPane();
Button btn1 = new Button(" 1 ");
Button btn2 = new Button("22222222");
root.getChildren().addAll(btn2, btn1);
root.setStyle("-fx-background-color: #87CEFA;");

網格窗格

GridPane允許您創建行和列的靈活網格,並將每個節點放置在准確的位置。

 GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setMinSize(300, 300);
grid.setVgap(5);
grid.setHgap(5);

Text username = new Text("Username:");
grid.add(username, 0, 0);

TextField text = new TextField();
text.setPrefColumnCount(10);
grid.add(text, 1, 0);

Text password = new Text("Password:");
grid.add(password, 0, 1);

TextField text2 = new TextField();
text2.setPrefColumnCount(10);
grid.add(text2, 1, 1);
grid.setStyle("-fx-background-color: #D8BFD8");

流板

流窗格按添加順序一個接一個地放置所有節點。

FlowPane flow = new FlowPane();
flow.setPadding(new Insets(10, 10, 10, 10));
flow.setStyle("-fx-background-color: DAE6F3;");
flow.setHgap(5);
flow.getChildren().addAll(left, center);

瓷磚

TilePane與流窗格類似。 所有節點都按照添加順序相同的順序放置在網格中。

TilePane tile = new TilePane();
tile.setPadding(new Insets(10, 10, 10, 10));
tile.setPrefColumns(2);
tile.setStyle("-fx-background-color: #CD5C5C;");
HBox hbox2 = new HBox(8); // spacing = 8
hbox2.getChildren().addAll(top, left, center);
tile.getChildren().add(hbox2);

錨板

AnchorPane允許您將節點放置在窗格的頂部,底部,左側,右側或中心。

AnchorPane anchorpane = new AnchorPane();
Button buttonSave = new Button("Save");
Button buttonCancel = new Button("Cancel");
anchorpane.setStyle("-fx-background-color: #A9A9A9;");
HBox hb = new HBox();
hb.getChildren().addAll(buttonSave, buttonCancel);
anchorpane.getChildren().addAll(hb);
anchorpane.setMinSize(300, 100);
AnchorPane.setRightAnchor(hb, 10.0);

BorderPane

BorderPane將場景分為五個區域,例如:頂部,底部,左側,右側和中央。 您可以在其中調整添加的節點。 BorderPane還允許您在每個區域中添加不同的窗格,如我的示例所示。 但是,您不能多次使用同一窗格。

 BorderPane pane = new BorderPane();
pane.setLeft(anchorpane);
pane.setCenter(root);
pane.setRight(grid);
pane.setTop(flow);
pane.setBottom(tile);

Scene scene = new Scene(pane, 300, 250);

暫無
暫無

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

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