Javafx最大化/最小化破坏了我的布局

标签 java user-interface javafx

我正在开发一个 javafx 游戏,布局正是我想要的。 然而,由于宽度和高度绑定(bind),布局仅在我调整窗口大小后才应用。当应用程序启动或最小化或最大化时,布局会变得困惑。

布局困惑后,只需调整一下大小即可恢复正常。

我尝试监听最大化和最小化事件,并使用与调整大小时使用的相同的更新方法,但它不起作用。

        primaryStage.maximizedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
            gamePane.updatePlayerPanes();
        }
    });

我真的迷失了。我尝试了很多方法,但没有成功,最小化或最大化时布局总是变得困惑。

刚刚开始: https://prnt.sc/jf2akp

稍微调整大小后: http://prntscr.com/jf2b19

我主要使用 GridPane 并使用 StackPane 作为中间 Pane 。

任何想法都表示赞赏,谢谢。

编辑:

这是一个模拟问题的简单类

刚刚开始:http://prntscr.com/jfb4lm

稍微调整大小后:http://prntscr.com/jfb4ud

   package test;
   import javafx.application.Application;
   import javafx.geometry.HPos;
   import javafx.geometry.VPos;
   import javafx.scene.Scene;
   import javafx.scene.image.Image;
   import javafx.scene.image.ImageView;
   import javafx.scene.layout.BorderPane;
   import javafx.scene.layout.ColumnConstraints;
   import javafx.scene.layout.GridPane;
   import javafx.scene.layout.Priority;
   import javafx.scene.layout.RowConstraints;
   import javafx.scene.layout.StackPane;
   import javafx.stage.Stage;
   public class Test extends Application
   {
       @Override
       public void start(Stage primaryStage)
       {
           BorderPane bp = new BorderPane();
           GridPane gp = new GridPane();
           gp.prefHeight(500);
           gp.prefWidth(500);
           for (int i = 0; i < 5; i++) {
               gp.getColumnConstraints().add(new ColumnConstraints(50, 50, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true));
           }
           for (int i = 0; i < 5; i++) {
               gp.getRowConstraints().add(new RowConstraints(50, 50, Double.MAX_VALUE, Priority.ALWAYS, VPos.CENTER, true));
           }
           bp.setCenter(gp);
           //Simulate the center pane
           StackPane centerPane = new StackPane();
           centerPane.setStyle("-fx-background-color: Green");
           //Add a bunch of cards
           for(int i =0; i < 10; i++)
           {
               Image img = new Image("file:AS_.png");
               ImageView imgView = new ImageView(img);
               imgView.setPreserveRatio(true);
                      imgView.fitHeightProperty().bind(centerPane.heightProperty().divide(2));
               imgView.setRotate(Math.random()*180);
               centerPane.getChildren().add(imgView);
           }
           gp.add(centerPane, 1, 1, 3, 3);
           Scene scene = new Scene(bp, 300, 300);
           primaryStage.setScene(scene);
           primaryStage.show();
       }
       public static void main(String[] args)
       {
           launch(args);
       }
   }

有没有办法强制调整大小或其他什么?

最佳答案

通过在primaryStage.show();后面添加这两行,问题似乎已经解决了。希望对您的实际应用有所帮助

GridPane.setFillHeight(centerPane, true);
GridPane.setFillWidth(centerPane, true);

您的测试类:

package test;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane bp = new BorderPane();
        GridPane gp = new GridPane();
        gp.prefHeight(500);
        gp.prefWidth(500);
        for (int i = 0; i < 5; i++) {
            gp.getColumnConstraints()
                    .add(new ColumnConstraints(50, 50, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true));
        }
        for (int i = 0; i < 5; i++) {
            gp.getRowConstraints()
                    .add(new RowConstraints(50, 50, Double.MAX_VALUE, Priority.ALWAYS, VPos.CENTER, true));
        }
        bp.setCenter(gp);
        // Simulate the center pane
        StackPane centerPane = new StackPane();
        centerPane.setStyle("-fx-background-color: Green");
        // Add a bunch of cards
        for (int i = 0; i < 10; i++) {
            Image img = new Image("file:AS_.png");
            ImageView imgView = new ImageView(img);
            imgView.setPreserveRatio(true);
            imgView.fitHeightProperty().bind(centerPane.heightProperty().divide(2));
            imgView.setRotate(Math.random() * 180);
            centerPane.getChildren().add(imgView);
        }
        gp.add(centerPane, 1, 1, 3, 3);
        Scene scene = new Scene(bp, 300, 300);
        primaryStage.setScene(scene);
        primaryStage.show();

        GridPane.setFillHeight(centerPane, true);
        GridPane.setFillWidth(centerPane, true);

    }

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

关于Javafx最大化/最小化破坏了我的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50224292/

相关文章:

java - 如何清除android中的缓存数据

java - 在java中声明一个 boolean 方法

java - 反编译其中包含嵌套类的类

Java 2D 数组,分配的是随机值,而不是应该分配的值

javascript - jQuery 日期选择器 : restrict date selection based on week day

c# - 用 C# 开发应用程序的最佳第 3 方 GUI 框架是什么?

iphone - 完善用户界面

java - 客户端麦克风笔记分析

JavaFX SegmentedButton - 全宽按钮

JavaFX 在 EventHandler 中使用父类(super class)