【发布时间】:2018-12-11 22:36:45
【问题描述】:
我正在尝试构建一个简单的 Java 项目,其中包含 6 个窗格(作为父级添加到 GridPane 布局)。我必须在开始时设置窗口大小,并通过参考根布局的宽度和高度设法将它们平均分割。
pane.setMinSize(root.getWidth()/3, root.getHeight()/2);
但我希望它们在我更改窗口大小时调整大小(使用鼠标,现在它们得到固定大小)。
所以我的问题是,我怎样才能在 JavaFX 中做到这一点?如何使元素拉伸并填充布局/部分布局的大小,然后在调整窗口大小时更改大小?
这是我的代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main6 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();
Scene scene = new Scene(root, 900, 600);
Pane pane1 = new Pane();
pane1.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
pane1.setMinSize(root.getWidth()/3, root.getHeight()/2);
Pane pane2 = new Pane();
pane2.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
pane2.setMinSize(root.getWidth()/3, root.getHeight()/2);
Pane pane3 = new Pane();
pane3.setBackground(new Background(new BackgroundFill(Color.GREY, CornerRadii.EMPTY, Insets.EMPTY)));
pane3.setMinSize(root.getWidth()/3, root.getHeight()/2);
Pane pane4 = new Pane();
pane4.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));
pane4.setMinSize(root.getWidth()/3, root.getHeight()/2);
Pane pane5 = new Pane();
pane5.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
pane5.setMinSize(root.getWidth()/3, root.getHeight()/2);
Pane pane6 = new Pane();
pane6.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
pane6.setMinSize(root.getWidth()/3, root.getHeight()/2);
root.getChildren().addAll(pane1, pane2, pane3, pane4, pane5, pane6);
GridPane.setRowIndex(pane1, 0);
GridPane.setColumnIndex(pane1, 0);
GridPane.setRowIndex(pane2, 0);
GridPane.setColumnIndex(pane2, 1);
GridPane.setRowIndex(pane3, 0);
GridPane.setColumnIndex(pane3, 2);
GridPane.setRowIndex(pane4, 1);
GridPane.setColumnIndex(pane4, 0);
GridPane.setRowIndex(pane5, 1);
GridPane.setColumnIndex(pane5, 1);
GridPane.setRowIndex(pane6, 1);
GridPane.setColumnIndex(pane6, 2);
primaryStage.setTitle("Learning JavaFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
您可能需要
VBox或HBox。
标签: javafx layout resize gridpane pane