【问题标题】:what is the best way to change scene with fxml controllers?使用 fxml 控制器更改场景的最佳方法是什么?
【发布时间】:2014-05-10 14:32:17
【问题描述】:

我有两个类 --menu 和 --game 他们都使用 fxml 文件来加载他们的场景场景到游戏类中的新场景

菜单:

    public class menu extends application{

     ....
    public void start(Stage stage) throws IOException{     
    StackPane loader = FXMLLoader.load(getClass().getResource("layout/startUp.fxml"));
    stage.setScene(new Scene(loader));
    stage.show();//show stage
   }
    ....
   }

游戏:

    public class game{
        public void start(Stage stage) throws IOException{

                StackPane root = FXMLLoader.load(getClass().getResource("layout/game.fxml"));
                Scene scene = new Scene(root);
                stage.setScene(scene);
            }
         }

菜单控制器:

public class menuController implements Initializable{

@FXML Button playButton;


@Override
public void initialize(URL location, ResourceBundle resources) {
    playButton.setOnAction(new EventHandler<ActionEvent>() {

         @Override
         public void handle(ActionEvent event) {
            /*
             now i want to do something like this
             */
          game g = new game();
          g.start(stage);//this stage is from menu
         }
        });

}

有什么想法吗?在此先感谢

【问题讨论】:

标签: javafx


【解决方案1】:

我解决了我的问题 我的错误是这样的:

我在我的 fxml 文件中使用 fx:controller 来设置我的控制器我删除了它,因为我需要将阶段传递给我的 menuController,我在菜单类的 start 方法中使用 setController 方法设置控制器并将new menuController(stage) 给 setController方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2013-03-13
    • 2010-11-19
    相关资源
    最近更新 更多