How do I make the program change the scene based on one button action in Javafx with Scene Builder?

Asked

Viewed 1,048 times

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 the other 2 players mode. When one of the modes is selected I intend to close this scene and open another, but so far I have not been able to do it. I’m new to java programming, so maybe it’s just a rookie mistake, but I’d appreciate it if you could help 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);
    }

}

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

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

Modo1controller - Modo1 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) {

   }    

}

Thanks in advance.

1 answer

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.