gpt4 book ai didi

java - 为什么 JavaFX 舞台/窗口/应用程序在更改场景后不刷新?

转载 作者:行者123 更新时间:2023-12-02 02:39:59 26 4
gpt4 key购买 nike

我将工作代码分为 2 个文件以避免困惑。以前确实有效,但是将所有场景都放在一个类中是非常不愉快的。

以前,当您单击 Sprite 时,它会将您从菜单带到游戏。然后我将游戏组和游戏场景的代码提取到游戏类。

现在,当我按下 Sprite 时,我可以看到它显示“单击”。这意味着改变场景正在发挥作用。 问题是在我分割文件后,第二个场景(游戏场景)显示第一个场景的内容而不是它自己的内容。 可能没有重新绘制。我该如何修复它?谢谢

主类代码(菜单窗口):

import javafx.application.Application; 
import static javafx.application.Application.launch;
import javafx.event.EventHandler;

import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

import javafx.stage.Stage;

public class EventFiltersExample extends Application {
@Override
public void start(Stage stage) {

Image playerImage = new Image("body.png");

ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);

Text text = new Text("Zegelardo");
text.setFont(Font.font(null, FontWeight.BOLD, 40));
text.setX(150);
text.setY(50);

Group menuGroup = new Group(playerImageView,text);

//Group gameGroup = new Group();

Scene menuScene = new Scene(menuGroup, 600, 300);
//Scene gameScene = new Scene(gameGroup, 600, 300);

stage.setTitle("Zegelardo");
stage.setScene(menuScene);
stage.show();

GameGroup gamegroup = new GameGroup();

EventHandler <MouseEvent> eventHandler = new EventHandler <MouseEvent>() {
@Override
public void handle(MouseEvent e) {
stage.setScene(gamegroup.gameScene);
System.out.println("Clicked.");
}
};

playerImageView.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
}
public static void main(String args[]){
launch(args);
}
}

具有游戏窗口构造函数/方法的类的代码:

import javafx.application.Application; 
import static javafx.application.Application.launch;
import javafx.event.EventHandler;

import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.Parent;
import javafx.scene.Group ;
import javafx.scene.Parent ;
import javafx.scene.shape.Line ;
import javafx.stage.Stage;

public class GameGroup {
public Group gameGroup;
public Scene gameScene;

public GameGroup() {
Image playerImage = new Image("body.png");

ImageView playerImageView = new ImageView(playerImage);
playerImageView.setX(50);
playerImageView.setY(25);

Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);




}

public Parent getView() {
return gameGroup ;
}
}

最佳答案

在您的 GameGroup 构造函数中,您新建了两个局部变量,而不是初始化字段。

更改 GameGroup.java

Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);

gameGroup = new Group(playerImageView);
gameScene = new Scene(gameGroup, 600, 300);

关于java - 为什么 JavaFX 舞台/窗口/应用程序在更改场景后不刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45660380/

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