在 FXML 控制器类中找不到 id 'wall' Java fx Scenebuilder 的可注入字段

问题描述 投票:0回答:1

我正在为我的大学项目编写一个迷宫。 我创建了一些场景,您必须单击“播放”才能继续,然后插入您的姓名,然后代码将创建迷宫等。 我在场景构建器和 FXML 文件方面遇到了一些问题,当我在场景构建器中创建一个矩形然后在 java 中使用相同的 id 调用它时,我收到此错误:无法调用“javafx.scene.shape.Rectangle.setX(double)”因为“this.wall”为空我检查了所有内容,但我找不到问题,在场景生成器中,场景设置了控制器类,它也适用于其他场景,只是在这个场景生成器中找不到可注射场。 这是我的代码(Labirynth Create 中没有任何内容,但我已经有一些代码来创建游戏,这是为了让您理解我的问题而简化的) 非常感谢。

package com.maze.Models;

import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
import javafx.animation.AnimationTimer;
import javafx.scene.input.KeyCode;


public class Player {


    private BooleanProperty upPressed = new SimpleBooleanProperty();
    private BooleanProperty leftPressed = new SimpleBooleanProperty();
    private BooleanProperty downPressed = new SimpleBooleanProperty();
    private BooleanProperty rightPressed = new SimpleBooleanProperty();
    private BooleanProperty upRight = new SimpleBooleanProperty();
    private BooleanProperty upLeft = new SimpleBooleanProperty();
    private BooleanProperty downRight = new SimpleBooleanProperty();
    private BooleanProperty downLeft = new SimpleBooleanProperty();

    private BooleanBinding keyPressed = upPressed.or(leftPressed).or(downPressed).or(rightPressed).or(upRight).or(upLeft).or(downRight).or(downLeft);

    int row;
    int col;

   public Player ()
    {
        row=0;
        col=0;
    }

    @FXML
    private ImageView sprite;

    @FXML
    private Group scene;



    public void makeMovable(ImageView sprite, Group scene){
        this.sprite = sprite;
        this.scene = scene;

        movementSetup();

        keyPressed.addListener(((observableValue, aBoolean, t1) -> {
            if(!aBoolean){
                timer.start();
            } else {
                timer.stop();
            }
        }));
    }

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long timestamp) {

            if(upPressed.get()) {

                if (row>=0 && row<9)
                {

                    sprite.setLayoutY(sprite.getLayoutY() - 75);
                    upPressed.set(false);
                    row++;
                }
                upPressed.set(false);

            }

            if(downPressed.get()){

                if (row>0 && row<=9)
                {
                    sprite.setLayoutY(sprite.getLayoutY() + 75);
                    downPressed.set(false);
                    row--;
                }
                downPressed.set(false);

            }

            if(leftPressed.get()){

                if (col>0 && col<=9)
                {
                    sprite.setLayoutX(sprite.getLayoutX() - 75);
                    leftPressed.set(false);
                    col--;
                }
                leftPressed.set(false);
            }

            if(rightPressed.get()){

                if (col>=0 && col<9)
                {
                    sprite.setLayoutX(sprite.getLayoutX() + 75);
                    rightPressed.set(false);
                    col++;
                }
                rightPressed.set(false);
            }


            if(upRight.get()){

                if (col>=0 && col<9 && row>=0 && row<9)
                {

                    sprite.setLayoutX(sprite.getLayoutX() + 75);
                    sprite.setLayoutY(sprite.getLayoutY() - 75);
                    upRight.set(false);
                    col++;
                    row++;
                    System.out.println("Posizione attuale"+row+col);
                }
                upRight.set(false);
            }

            if(upLeft.get()){

                if (col>0 && col<=9 && row>=0 && row<9)
                {
                    sprite.setLayoutX(sprite.getLayoutX() - 75);
                    sprite.setLayoutY(sprite.getLayoutY() - 75);
                    upLeft.set(false);
                    col--;
                    row++;
                    System.out.println("Posizione attuale"+row+col);
                }
                upLeft.set(false);
            }

            if(downRight.get()){

                if (row>0 && row<=9 && col>=0 && col<9)
                {
                    sprite.setLayoutX(sprite.getLayoutX() + 75);
                    sprite.setLayoutY(sprite.getLayoutY() + 75);
                    downRight.set(false);
                    col++;
                    row--;
                    System.out.println("Posizione attuale"+row+col);
                }
                downRight.set(false);
            }

            if(downLeft.get()){

                if (col>0 && col<=9 && row>0 && row<=9)
                {
                    sprite.setLayoutX(sprite.getLayoutX() - 75);
                    sprite.setLayoutY(sprite.getLayoutY() + 75);
                    downLeft.set(false);
                    col--;
                    row--;
                    System.out.println("Posizione attuale"+row+col);
                }
                downLeft.set(false);
            }


        }
    };

    private void movementSetup(){
        scene.setOnKeyPressed(e -> {
            if(e.getCode() == KeyCode.W) {
                upPressed.set(true);
            }

            if(e.getCode() == KeyCode.A) {
                leftPressed.set(true);
            }

            if(e.getCode() == KeyCode.S) {
                downPressed.set(true);
            }

            if(e.getCode() == KeyCode.D) {
                rightPressed.set(true);
            }
            ///
            if(e.getCode() == KeyCode.E) {
                upRight.set(true);
            }

            if(e.getCode() == KeyCode.Q) {
                upLeft.set(true);
            }

            if(e.getCode() == KeyCode.C) {
                downRight.set(true);
            }

            if(e.getCode() == KeyCode.Z) {
                downLeft.set(true);
            }
        });

    }
}

package com.maze.Models;

public class User {


       private String name, surname;



        public void setName(String name) {
            this.name = name;
        }
        public void setSurname(String surname) {
            this.surname = surname;
        }
        public String getName() {
            return name;
        }
        public String getSurname() {
            return surname;
        }

}
package com.maze.Models;

public class Model {
    private static Model instance;
    private User user;

    public static synchronized Model getInstance(){
        if (instance == null)
        {
            instance = new Model();
        }
        return instance;
    }

    private Model() {
        this.user= new User();
    }

    public void createUser (String name, String surname)
    {
        user.setName(name);
        user.setSurname(surname);
        System.out.println(user.getName());
        System.out.println(user.getName());
    }



}
package com.maze.Models;

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import java.lang.Cloneable;
import java.lang.Object;
public class Cella implements Cloneable {

    private Rectangle rect;


    public Cella()
    {
        rect = new Rectangle();
        rect.setX(75);
        rect.setY(75);
        rect.setHeight(70);
        rect.setWidth(70);
        rect.setFill(Color.GRAY);
    }
    public Cella (Cella temp)
    {
        temp.rect.setX(this.rect.getX()*2);
        temp.rect.setY(this.rect.getY()*2);
        temp.rect.setHeight(this.rect.getHeight());
        temp.rect.setWidth(this.rect.getWidth());
        temp.rect.setFill(this.rect.getFill());
    }

    public Rectangle getRectangle(){
        return rect;
    }

    public void editX(int x)
    {
        rect.setX(x);
    }

    public void editY(int y)
    {
        rect.setY(y);
    }

    @Override
    public Object clone() throws CloneNotSupportedException
    {
      Cella temp= new Cella();
      temp.rect.setX(this.rect.getX());
      temp.rect.setY(this.rect.getY());
      temp.rect.setHeight(this.rect.getHeight());
      temp.rect.setWidth(this.rect.getWidth());
      temp.rect.setFill(this.rect.getFill());
      return temp;
    }


}


package com.maze.Views;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.scene.Parent;

import java.io.IOException;

public class LabyrinthApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource(
                "/com/maze/FXML/Scene1.fxml"));

        Parent root = loader.load();
        Scene scene = new Scene(root,1000,900);




        stage.setTitle("Labyrinth");
        stage.setScene(scene);
        stage.show();
        stage.setResizable(false);
    }

    public static void main(String[] args) {
        launch();
    }
}
package com.maze.ViewModels;

import com.maze.Models.Model;
import com.maze.Models.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;



public class LabyrinthName {
    @FXML
    private TextField nametext;
    @FXML
    private TextField surnametext;
    @FXML
    private Button submitbutton;


    String Name, Surname;
    LabyrinthCreate create = new LabyrinthCreate();

    @FXML

    public void submit(ActionEvent event) throws Exception
    {
        Name = nametext.getText();
        Surname = surnametext.getText();
        Model.getInstance().createUser(Name,Surname);
        FXMLLoader loader = new FXMLLoader(getClass().getResource(
                "/com/maze/FXML/Scene3.fxml"));
        Parent root = loader.load();
        Stage window = (Stage) submitbutton.getScene().getWindow();

        window.setScene(new Scene(root,1000,900));
        window.setResizable(false);
        create.createLab(window);
    }

}

    package com.maze.ViewModels;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class LabyrinthController {
    @FXML
    Button playbutton;

    public void onPlayButtonClick() throws Exception {

        FXMLLoader loader = new FXMLLoader(getClass().getResource(
                "/com/maze/FXML/Scene2.fxml"));
        Parent root = loader.load();
        Stage window = (Stage) playbutton.getScene().getWindow();
        window.setScene(new Scene(root,1000,900));


        window.setResizable(false);

    }
}


package com.maze.ViewModels;

import javafx.fxml.FXML;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class LabyrinthCreate
{


    @FXML
    private Rectangle wall;

    public void createLab(Stage primaryStage) throws Exception
    {


        wall.setX(100);
        wall.setY(100);

        System.out.println("tutto ok");
        primaryStage.show();
    }
}

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="900.0" spacing="20.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/20.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.maze.ViewModels.LabyrinthController">
  <padding>
    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
  </padding>
   <children>
      <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="900.0">
         <children>
            <Text fill="WHITE" layoutX="183.0" layoutY="306.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MAZE GAME ">
               <font>
                  <Font name="Cooper Black" size="64.0" />
               </font>
            </Text>
            <Button fx:id="playbutton" layoutX="326.0" layoutY="391.0" mnemonicParsing="false" onAction="#onPlayButtonClick" prefHeight="63.0" prefWidth="218.0" style="-fx-background-color: grey;" text="Play" textAlignment="CENTER" textFill="WHITE">
               <font>
                  <Font name="Cooper Black" size="46.0" />
               </font>
               <effect>
                  <DropShadow height="103.07" offsetX="6.0" radius="40.775" width="62.03">
                     <color>
                        <Color red="0.32894736528396606" green="0.3259015679359436" blue="0.3259015679359436" />
                     </color>
                  </DropShadow>
               </effect>
            </Button>
            <Text fill="WHITE" layoutX="513.0" layoutY="771.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Created by Antonella Sannino">
               <font>
                  <Font name="Cooper Black" size="24.0" />
               </font>
            </Text>
            <ImageView fx:id="startRobot" fitHeight="150.0" fitWidth="200.0" layoutX="617.0" layoutY="192.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../image/robot.png" />
               </image>
            </ImageView>
         </children>
      </AnchorPane>
   </children>
</VBox>

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="900.0" spacing="20.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/20.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.maze.ViewModels.LabyrinthName">
  <padding>
    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
  </padding>
   <children>
      <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="900.0">
         <children>
            <Button fx:id="submitbutton" alignment="CENTER" contentDisplay="CENTER" layoutX="399.0" layoutY="472.0" mnemonicParsing="false" onAction="#submit" prefHeight="49.0" prefWidth="102.0" text="Submit">
               <font>
                  <Font name="System Bold" size="20.0" />
               </font>
            </Button>
            <TextField fx:id="nametext" layoutX="283.0" layoutY="360.0" style="-fx-alignment: center;">
               <font>
                  <Font name="Arial Bold" size="12.0" />
               </font>
            </TextField>
            <Text fill="WHITE" layoutX="310.0" layoutY="344.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Name">
               <font>
                  <Font name="Cooper Black" size="33.0" />
               </font>
            </Text>
            <TextField fx:id="surnametext" alignment="CENTER_RIGHT" layoutX="466.0" layoutY="360.0" style="-fx-alignment: center;">
               <font>
                  <Font name="Arial Bold" size="12.0" />
               </font>
            </TextField>
            <Text fill="WHITE" layoutX="465.0" layoutY="343.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Surname">
               <font>
                  <Font name="Cooper Black" size="33.0" />
               </font>
            </Text>
            <Text fill="WHITE" layoutX="306.0" layoutY="235.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Insert your name">
               <font>
                  <Font name="Cooper Black" size="33.0" />
               </font>
            </Text>
         </children>
      </AnchorPane>
   </children>
</VBox>

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

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="900.0" prefWidth="1000.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.maze.ViewModels.LabyrinthCreate">
   <children>
      <Rectangle fx:id="wall" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
   </children>
</VBox>

Scenebuilder view

我期待场景构建器找到 Rectangle 类的 id。

java javafx scenebuilder
1个回答
0
投票

在 labirynthName 中我需要添加 loader.getController().createLab(窗口);

© www.soinside.com 2019 - 2024. All rights reserved.