How to make the program change the scene based on the action of a button in JavaFX with Scene Builder?

1

Hello everyone. I am trying to create a game in which the first window displays 2 buttons. One must activate 1 player mode and another 2 player mode. When one of the modes is selected, I want to close this scene and open another one, but so far I have not been able to do so. I'm new to programming in java, so maybe it's just a beginner's bug, but thanks for helping me solve it.

FXMLController

package Rastros;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class FXMLDocumentController implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    public void MudarEstado1() throws IOException {
        System.out.println("Modo 1 jogador ativado");
        rastros r1 = new rastros();
        r1.Estado(1);

     }

    public void MudarEstado2() throws IOException {
        System.out.println("Modo 2 jogadores ativado");
        rastros r1 = new rastros();
        r1.Estado(2);
    }

}

Trails.java - Main Class

package Rastros;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class rastros extends Application {    



    @Override
    public void start(Stage stage) throws Exception {       
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);
        stage.setTitle("Bem Vindo ao Rastros");

        stage.setScene(scene);
        stage.show();


    }

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {
        launch(args);

    }

    public void Estado(int estado) throws IOException {

        switch (estado) {
            case 1:
                System.out.println("Estado 1");
                //Aqui quero mudar a cena
                break;
            case 2:
                System.out.println("Estado 2");
                //Aqui quero mudar para uma cena diferente da do estado mas neste caso não interessa
                break;
        }
    } 

}

FXMLDocument

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="508.0" prefWidth="683.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Rastros.FXMLDocumentController">
<children>
      <ImageView fx:id="Background" fitHeight="508.0" fitWidth="684.0">
         <image>
            <Image url="@../../../../../Desktop/Background.jpg" />
         </image>
    </ImageView>
    <Label fx:id="LblHall" alignment="CENTER" layoutY="85.0" prefHeight="76.0" prefWidth="684.0" text="Bem-Vindo ao Rastros!" textAlignment="CENTER" textFill="#c9c9c9">
         <font>
            <Font size="25.0" />
         </font>
    </Label>
    <Button fx:id="BtnModo1" layoutX="265.0" layoutY="204.0" mnemonicParsing="false" onAction="#MudarEstado1" prefHeight="69.0" prefWidth="155.0" text="1 Jogador" />
    <Button fx:id="BtnModo2" layoutX="265.0" layoutY="328.0" mnemonicParsing="false" onAction="#MudarEstado2" prefHeight="69.0" prefWidth="155.0" text="2 Jogadores" />
</children>
</AnchorPane>

Mode1.fxml - Scene where I want to change

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Rastros.Modo1Controller">
 <children>
     <Label alignment="CENTER" layoutX="221.0" layoutY="140.0" prefHeight="47.0" prefWidth="158.0" text="Modo 1 jogador ativado" textAlignment="CENTER" />
 </children>
</AnchorPane>

Controller Mode - Mode1 Controller

package Trails;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

public class Modo1Controller implements Initializable {

 @Override
   public void initialize(URL url, ResourceBundle rb) {

   }    

}

Thank you in advance.

    
asked by anonymous 16.04.2017 / 18:53

1 answer

-1