【问题标题】:Javafx maximize/minimize ruins my layoutJavafx 最大化/最小化破坏了我的布局
【发布时间】:2018-05-08 00:49:18
【问题描述】:

我正在开发一个 javafx 游戏,而布局正是我想要的。 但是,由于我的宽度和高度绑定,布局仅在我调整窗口大小后应用。当应用启动或最小化或最大化时,布局会变得混乱。

在布局混乱之后,只需稍微调整大小即可使其恢复正常。

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

        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 作为中间窗格。

感谢任何想法。

编辑:

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

刚开始: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);
       }
   }

有什么方法可以强制调整大小吗?

【问题讨论】:

  • 你能把关于你的gridpane和stackpane的代码贴出来
  • 它有 500 行很长,有什么具体要找的吗?某些方法或侦听器?
  • 您能否使用简单(基本)组件编写一个简单且可验证的代码,这些组件会产生相同的问题(当然还有相同的布局)
  • 完成!您可以在编辑中找到简单的测试代码。感谢您的关注。
  • 有趣的是,删除图像视图中使用的绑定后一切都很好。我需要绑定我的图像高度,但还有其他方法吗?

标签: java user-interface javafx


【解决方案1】:

通过在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);
    }
}

【讨论】:

  • 感谢您的宝贵时间。一开始确实有点帮助,元素还没有完全到位,但不知何故它们更接近了。但是最大化和最小化仍然是相同的。有没有办法强制触发调整大小事件或重新计算位置或重绘场景或其他东西?
猜你喜欢
  • 1970-01-01
  • 2021-07-14
  • 2023-03-23
  • 2011-01-21
  • 2023-03-24
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
相关资源
最近更新 更多