Domanda

i have two class --menu and --game both of them use a fxml file to load their scene i have a button in menu and i want if user clicked on button, my stage in menu ,passes to game and change scene to the new scene in game class

menu :

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

game:

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

menu controller:

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

}

any ideas?thanks in advance

È stato utile?

Soluzione

i solve my problem my mistake was this:

i used fx:controller in my fxml file to set my Controller i remove that because i need to pass stage to my menuController , i used setController method for set controller in my start method in menu class and give new menuController(stage) to setController method

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top