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

时间:2017-08-13 12:31:37

标签: java javafx

我将2个文件的工作代码分开以避免混乱。它之前有用,但是把所有场景都放在一个班级里是非常不愉快的。

在您点击精灵之前,它会从菜单到游戏。然后我将游戏组和gamescene的代码提取到游戏类中。

现在我可以看到它说"点击"当我按下精灵。这意味着改变场景是有效的。 问题是我分割文件后第二个场景(游戏场景)显示第一个场景的内容而不是它自己的内容。 可能不重新粉刷。我该如何解决?感谢

主类的代码(菜单窗口):

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 ;
        }
}

1 个答案:

答案 0 :(得分:2)

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);