gpt4 book ai didi

JavaFX GUI因切换场景而卡住

转载 作者:行者123 更新时间:2023-11-30 06:22:56 25 4
gpt4 key购买 nike

我正在将游戏场景从启动页面切换到游戏中,如果我单独玩游戏场景,效果很好,但是当我从启动页面切换后玩游戏时,游戏会卡住一点。

是什么导致我的主游戏在切换场景后卡住一段时间(一段时间后又重新开始),我确信游戏在不切换场景的情况下运行良好。

程序的控制流程是 - 从播放器将启动页面场景加载到舞台,在主 Controller 中,如果按下播放按钮,则场景将交换到我的游戏场景。该问题仅因切换场景而出现。请帮忙。提前致谢。

这是我的主要职业玩家

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class player extends Application {
Scene s1,s2;
Stage stg;
public static void main(String args[]) {
Application.launch(args);
}

@Override
public void start(Stage stage) throws Exception {
stg = stage;
FXMLLoader loader1 = new FXMLLoader(getClass().getResource("Main.fxml"));
Parent root1 = loader1.load();

s1 = new Scene(root1,800,600);

s1.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
stg.setScene(s1);

stg.show();

}
}

这是我的启动页面 Controller 。

package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class mainController {
@FXML
Button play;


public void func(ActionEvent event) throws Exception {
FXMLLoader loader = new FXMLLoader (getClass().getResource("thread1.fxml"));
Parent root = loader.load();
Scene s2 = new Scene(root,800,600);
Stage window = (Stage)(((Node)event.getSource()).getScene().getWindow());
window.setScene(s2);
window.show();
}
}

这是我的启动页面 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<Pane id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.mainController">
<children>
<ImageView id="menubg" fitHeight="1193.0" fitWidth="763.0" layoutY="-231.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@background.png" />
</image>
</ImageView>
<Button fx:id="play" layoutX="265.0" layoutY="322.0" mnemonicParsing="false" onAction="#func" prefHeight="40.0" prefWidth="111.0" text="Play Game" textAlignment="CENTER" />
<Button fx:id="help" layoutX="241.0" layoutY="483.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="58.0" text="Help" textAlignment="CENTER" />
<Button fx:id="exit" layoutX="349.0" layoutY="483.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="58.0" text="Exit" textAlignment="CENTER" />
<Text id="title" layoutX="177.0" layoutY="231.0" strokeType="OUTSIDE" strokeWidth="0.0" text="PARACHUTE PANIC" textAlignment="CENTER">
<font>
<Font name="AR DARLING" size="31.0" />
</font>
</Text>
<Text id="highscore" layoutX="254.0" layoutY="433.0" strokeType="OUTSIDE" strokeWidth="0.0" text="HIGHSCORE: 40">
<font>
<Font name="AR DARLING" size="17.0" />
</font>
</Text>
</children>
</Pane>

游戏 Controller 类太大,无法上传

这是我的游戏 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<ImageView fx:id="background" fitHeight="1094.0" fitWidth="731.0" layoutX="-2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@background.png" />
</image>
</ImageView>
<ImageView fx:id="player1" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero1" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player2" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero2" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player3" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero3" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player4" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero4" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player5" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero5" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player6" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero6" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="cloud1" fitHeight="59.0" fitWidth="118.0" layoutX="-7.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud3" fitHeight="59.0" fitWidth="118.0" layoutX="150.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud4" fitHeight="59.0" fitWidth="118.0" layoutX="250.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud6" fitHeight="59.0" fitWidth="118.0" layoutX="340.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud7" fitHeight="59.0" fitWidth="118.0" layoutX="440.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud8" fitHeight="59.0" fitWidth="118.0" layoutX="540.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud9" fitHeight="59.0" fitWidth="118.0" layoutX="640.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="boat" fitHeight="160" fitWidth="160" layoutX="0.0" layoutY="665.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@boat.png" />
</image>
</ImageView>
<ImageView fx:id="water1" fitHeight="95.0" fitWidth="400.0" layoutX="386.0" layoutY="680.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@water.gif" />
</image>
</ImageView>
<ImageView fx:id="water" fitHeight="95.0" fitWidth="400.0" layoutX="-10.0" layoutY="683.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@water.gif" />
</image>
</ImageView>
<ImageView fx:id="plane" fitHeight="95" fitWidth="95" layoutX="650.0" layoutY="31.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@airplane.png" />
</image>
</ImageView>
<ImageView fx:id="cloud2" fitHeight="59.0" fitWidth="118.0" layoutX="60.0" layoutY="-5.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud5" fitHeight="59.0" fitWidth="118.0" layoutX="190.0" layoutY="-17.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<ImageView fx:id="cloud10" fitHeight="59.0" fitWidth="118.0" layoutX="590.0" layoutY="-9.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@cloud.png" />
</image>
</ImageView>
<Label fx:id="lives" layoutX="14.0" layoutY="6.0" text="Label">
<font>
<Font name="AR DARLING" size="18.0" />
</font></Label>
<Label fx:id="score" layoutX="579.0" layoutY="6.0" text="Label">
<font>
<Font name="AR DARLING" size="18.0" />
</font></Label>
</children>
</AnchorPane>

最佳答案

我猜想在单击按钮后从 JavaFX 线程内部加载 thread1.fxml 可能是问题所在。

您可以尝试在单击按钮之前加载它,这样就不会花费太长时间。

Player.java:

public class player extends Application {
Scene s1,s2;
Stage stg;
public static void main(String args[]) {
Application.launch(args);
}

@Override
public void start(Stage stage) throws Exception {
stg = stage;
FXMLLoader loader1 = new FXMLLoader(getClass().getResource("Main.fxml"));
Parent root1 = loader1.load();
s1 = new Scene(root1,800,600);
s1.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
stg.setScene(s1);

FXMLLoader loader2 = new FXMLLoader (getClass().getResource("thread1.fxml"));
Parent thread1Root = loader2.load();
s2 = new Scene(thread1Root,800,600);
((MainController)loader2.getController()).setThread1Scene(s2);

stg.show();
}
}

MainController.java:

public class MainController {
@FXML
Button play;
private Scene thread1Scene;

public void setThread1Scene(Scene s2) {
this.thread1Scene = s2;
}

public void func(ActionEvent event) throws Exception {
Stage window = (Stage)(((Node)event.getSource()).getScene().getWindow());
window.setScene(s2);
window.show();
}
}
<小时/>

您还可以在单​​击按钮时显示“正在加载”屏幕,然后在 Platform.runLater() 中加载 thread1.fxml:

showLoadingLabel();
Platform.runLater(() -> doSomethingSlow());

关于JavaFX GUI因切换场景而卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754958/

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