jeudi 30 avril 2015

JavaFX Scene wechseln unter FX

Heyho liebe Java'ler,

ich arbeite mich derzeitig in JavaFX ein und versuche relativ banale Dinge zu programmieren , verstehe aber derzeitig die Funktionsweise von FX nicht.

Derzeitig ist mein Problem ,dass ich 1 Menü habe das beim Klick auf "Spiel Starten" ein weiteres Menü mit "Singleplayer " und "Multiplayer" öffnen soll, hierbei funktioniert aber der scene wechseln nicht.

Java Code:

  1. package team;
  2.  
  3. import javafx.application.Application;
  4. import javafx.fxml.FXML;
  5. import javafx.fxml.FXMLLoader;
  6. import javafx.stage.Stage;
  7. import javafx.scene.Parent;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.layout.BorderPane;
  11.  
  12.  
  13. public class Menu extends Application {
  14.  
  15. @Override
  16. public void start(Stage primaryStage) {
  17.  
  18. // Showing Menu
  19.  
  20. try {
  21.  
  22. /*
  23. * Gets StyleSheets of MapSource.fxml (generated with JavaFX Scene
  24. * Builder 20
  25. */
  26.  
  27. /* Overwritten by "Parent root = [...]" */
  28. // BorderPane root = new BorderPane();
  29.  
  30. Parent rootOne = FXMLLoader.load(getClass().getResource(
  31. "menuSource.fxml"));
  32. Parent rootTwo = FXMLLoader.load(getClass().getResource(
  33. "menuStartSource.fxml"));
  34.  
  35. Scene menu = new Scene(rootOne, 500, 400);
  36. Scene menuStart = new Scene(rootTwo, 500, 400);
  37.  
  38. if (MenuController.szene == 0) {
  39. primaryStage.setScene(menu);
  40. } else if (MenuController.szene == 1) {
  41. primaryStage.setScene(menuStart);
  42. } else {
  43. primaryStage.setScene(menuStart);
  44. }
  45.  
  46. // Showing the actual scene
  47. primaryStage.show();
  48.  
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. public static void main(String[] args) {
  55. launch(args);
  56. }
  57.  
  58. }


Java Code:

  1. package team;
  2.  
  3. import javafx.fxml.FXML;
  4. import javafx.scene.control.Button;
  5.  
  6. public class MenuController {
  7. static int szene;
  8.  
  9. @FXML
  10. Button StartGame;
  11.  
  12. @FXML
  13. Button Options;
  14.  
  15. @FXML
  16. Button ExitGame;
  17.  
  18. @FXML
  19. Button Singleplayer;
  20.  
  21. @FXML
  22. Button Multiplayer;
  23.  
  24. @FXML
  25. Button Exit;
  26.  
  27. /*
  28. * szene: 0 = Menu 1 = StartGame 2 = Options 3 = Exit 4 = SingleplayerStart
  29. * 5 = MultiplayerStart
  30. */
  31.  
  32. @FXML
  33. protected void buttonPressedMenu() {
  34. szene = 0;
  35. }
  36.  
  37. @FXML
  38. protected void buttonPressedStartGame() {
  39. szene = 1;
  40. }
  41.  
  42. @FXML
  43. protected void buttonPressedOptions() {
  44. szene = 2;
  45. }
  46.  
  47. @FXML
  48. protected void buttonPressedExitGame() {
  49. szene = 3;
  50. System.out.print("SYSTEM TERMINATED");
  51. System.exit(0);
  52. }
  53.  
  54. @FXML
  55. protected void buttonPressedSingleplayer() {
  56. szene = 4;
  57. }
  58.  
  59. @FXML
  60. protected void buttonPressedMultiplayer() {
  61. szene = 5;
  62. }
  63. }


Code:

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

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

<AnchorPane xmlns="http://ift.tt/Ras5Ew; xmlns:fx="http://ift.tt/1pVISuK; fx:controller="team14.MenuController">
  <children>
      <GridPane layoutX="-1.0" layoutY="-1.0" prefHeight="186.0" prefWidth="309.0">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="135.0" minWidth="10.0" prefWidth="30.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="135.0" minWidth="10.0" prefWidth="111.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="194.0" minWidth="0.0" prefWidth="27.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="182.0" minWidth="10.0" prefWidth="121.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="130.0" minWidth="10.0" prefWidth="30.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints maxHeight="60.0" minHeight="10.0" prefHeight="31.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="60.0" minHeight="10.0" prefHeight="31.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="31.0" minHeight="0.0" prefHeight="29.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
        <children>
            <Button mnemonicParsing="false" onAction="#buttonPressedMultiplayer" prefHeight="35.0" prefWidth="117.0" text="Multiplayer" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Button mnemonicParsing="false" onAction="#buttonPressedSingleplayer" prefHeight="33.0" prefWidth="115.0" text="Singleplayer" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Button alignment="CENTER" mnemonicParsing="false" onAction="#buttonPressedMenu" prefHeight="25.0" prefWidth="85.0" text="Back" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
        </children>
      </GridPane>
  </children>
</AnchorPane>

Code:

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

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

<AnchorPane xmlns="http://ift.tt/Ras5Ew; xmlns:fx="http://ift.tt/1pVISuK; fx:controller="team14.MenuController">
  <children>
      <GridPane layoutX="-1.0" prefHeight="369.0" prefWidth="494.0">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="116.0" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="274.0" minWidth="10.0" prefWidth="269.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="110.0" minWidth="0.0" prefWidth="67.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
        <children>
            <Button mnemonicParsing="false" onAction="#buttonPressedStartGame" prefHeight="48.0" prefWidth="299.0" text="Start Game" GridPane.columnIndex="1" GridPane.rowIndex="1">
              <font>
                  <Font size="14.0" />
              </font></Button>
            <Button mnemonicParsing="false" onAction="#buttonPressedOptions" prefHeight="48.0" prefWidth="302.0" text="Options" GridPane.columnIndex="1" GridPane.rowIndex="2">
              <font>
                  <Font size="14.0" />
              </font></Button>
            <Button mnemonicParsing="false" onAction="#buttonPressedExitGame" prefHeight="48.0" prefWidth="298.0" text="Exit Game" GridPane.columnIndex="1" GridPane.rowIndex="3">
              <font>
                  <Font size="14.0" />
              </font></Button>
        </children>
      </GridPane>
  </children>
</AnchorPane>

Es ist derzeitig also wie folgt aufgebaut: Wird auf "Start Game" geklickt wird ein Integer Wert quasi als Leitwert der Szene auf 1 gesetzt , welches dann in der Start Methode das alles wechseln sollte.

Hoffe ihr könnt mir helfen..

Mit freundlichen Grüßen
Lacritz


JavaFX Scene wechseln unter FX

0 commentaires:

Enregistrer un commentaire