gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:09 24 4
gpt4 key购买 nike

我正在开发一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com