首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么改变场景后JavaFX阶段/窗口/应用程序不刷新?

为什么改变场景后JavaFX阶段/窗口/应用程序不刷新?
EN

Stack Overflow用户
提问于 2017-08-13 12:31:37
回答 1查看 591关注 0票数 0

为了避免混乱,我为两个文件划分了工作代码。它以前起过作用,但是把所有的场景都放在一节课上是很不愉快的。

之前,当你点击雪碧,它会带你从菜单到游戏。THen I用游戏组和游戏类提取代码。

现在我可以看到,当我按下雪碧时,上面写着“点击”。这意味着改变环境是可行的。问题是,把文件分割后,第二场景(游戏场景)显示的是第一个场景的内容,而不是自己的。可能没有重新粉刷。我怎么才能修好它?谢谢

主类代码(菜单窗口):

代码语言:javascript
复制
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); 
   } 
}

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

代码语言:javascript
复制
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 ;
        }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-13 13:18:20

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

将GameGroup.java改为

代码语言:javascript
复制
Group gameGroup = new Group(playerImageView);
Scene gameScene = new Scene(gameGroup, 600, 300);

代码语言:javascript
复制
gameGroup = new Group(playerImageView);
gameScene = new Scene(gameGroup, 600, 300);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45660380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档