[英]Maintain Gridpane the same height and width (Aspect ratio)
我有一個 javafx GridPane,當我調整舞台大小時,它會調整自身大小以適應舞台的尺寸。 我希望網格窗格的高度與網格窗格的寬度相匹配,從而將大小調整為保持此約束的最大可能大小。
我不得不在 GridPane 的父級中添加更多元素。 我發現添加的元素根本沒有正常行為,並且相互重疊。
當然,當我刪除覆蓋方法時,它們不再重疊,但是當我重新調整舞台大小時,網格窗格不會保持完美的正方形。
我想嘗試在背景中使用圖像/圖像視圖並為其應用setPreserveRatio(true)
。 然后將此圖像的 heightProperty 綁定到 gridPane 的 prefHeightProperty,但由於某種原因,這也沒有給我任何結果。
這是第二種方法的 MCVE,它不起作用,但如果我能以某種方式使它起作用,那就太好了。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
StackPane stackPane = new StackPane();
Image image = new Image("Square Image.png", 300, 300, true, true);
ImageView imageView = new ImageView(image);
// This makes the image resize while maintaining its square shape...
imageView.fitHeightProperty().bind(stackPane.heightProperty());
imageView.fitWidthProperty().bind(stackPane.widthProperty());
imageView.setPreserveRatio(true);
GridPane gridPane = new GridPane();
for(int i=0; i<5; i++) {
for (int j = 0; j < 5; j++) {
Pane pane = new Pane();
pane.setPrefSize(100, 100);
gridPane.add(pane, i, j);
}
}
// Does not work as intended... :(
gridPane.prefWidthProperty().bind(imageView.fitWidthProperty());
gridPane.prefHeightProperty().bind(imageView.fitHeightProperty());
/*
Tried this as well, also does not work.. :(
gridPane.prefWidthProperty().bind(image.widthProperty());
gridPane.prefHeightProperty().bind(image.heightProperty());
*/
gridPane.setGridLinesVisible(true);
stackPane.getChildren().addAll(imageView, gridPane);
primaryStage.setScene(new Scene(stackPane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
當然,所有這些方法都是試圖完成這項工作的廉價 hacky 方法。 如果有一種我不知道的標准方法,那么我想知道。
將GridPane
封裝在StackPane
中,並使用綁定動態更改GridPane
子項的首選大小,同時保持縱橫比:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Main extends Application {
private final int SIZE = 5;
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
StackPane centerPane = new StackPane(gridPane);
centerPane.setStyle("-fx-background-color:cyan");
centerPane.setPrefSize(SIZE*50, SIZE*50);
for(int i=0; i<SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
Pane pane = new Pane();
pane.setStyle("-fx-background-color:red");
gridPane.add(pane, i, j);
pane.prefWidthProperty().bind(Bindings.min(centerPane.widthProperty().divide(SIZE),
centerPane.heightProperty().divide(SIZE)));
pane.prefHeightProperty().bind(Bindings.min(centerPane.widthProperty().divide(SIZE),
centerPane.heightProperty().divide(SIZE)));
}
}
gridPane.setGridLinesVisible(true);
primaryStage.setScene(new Scene(centerPane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.