简体   繁体   English

尝试使用 JavaFX 添加按钮时出错

[英]Error while trying to add a button using JavaFX

I have a problem while trying to add a button on an HBox.尝试在 HBox 上添加按钮时遇到问题。 The error that I got is "Cannot invoke "javafx.scene.layout.HBox.getChildren()" because "this.buttonBox" is null" even though I already have an HBox with fx:id buttonBox.我得到的错误是“无法调用”javafx.scene.layout.HBox.getChildren()”,因为“this.buttonBox”为空”,即使我已经有一个带有 fx:id buttonBox 的 HBox。 Can someone help me with this?有人可以帮我弄这个吗?

And I also tried to add a new HBox on the code and then add a button but the button did not show up.而且我还尝试在代码上添加一个新的HBox,然后添加一个按钮,但该按钮没有显示出来。

Here is my code:这是我的代码:

package com.example.simplemaps;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import java.io.IOException;
import java.util.ArrayList;

public class View {

    public static final String INTERFACE_LOCATION = "maps-view.fxml";
    private static final Image IMG_BACKGROUND = new Image("com/example/simplemaps/AisleForward.jpg");
    private static final Image IMG_FOREGROUND = new Image("com/example/simplemaps/Bunny.png");
    //private Controller controller;

    @FXML private Label label;
    @FXML private ImageView imageBackground;
    @FXML private ImageView imageForeground;
    @FXML private HBox buttonBox;

    public View() {
        //controller = new Controller();
    }

    public void pickUp()
    {
        // TO DO
    }

    public void putDown()
    {
        imageForeground.setImage(IMG_FOREGROUND);
    }

    public void start()
    {
        try
        {
            FXMLLoader fxmlLoader = new FXMLLoader(SimpleMaps.class.getResource(INTERFACE_LOCATION));
            Parent anchorPane = fxmlLoader.load();
            SimpleMaps.mainStage.setScene(new Scene(anchorPane));

            SimpleMaps.mainStage.show();
        }

        catch(IOException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void addWhereToButtons(String exit) {
        //for(String exit : exits) {
            Button button = new Button(exit);
            HBox.setMargin(button, new Insets(10.0,0.0,10.0,5.0));
            buttonBox.getChildren().add(button);
            button.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                        //controller.selectExitDirection(exit);
                    }
                }
            );
        //}
    }

}

This is the error:这是错误:

/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home/bin/java --module-path /Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib --add-modules javafx.controls,javafx.fxml -Djava.library.path=/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51875:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/ulfianidian/Desktop/IPPO/SimpleMaps/target/classes:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx-swt.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.web.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.base.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.fxml.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.media.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.swing.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.controls.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.graphics.jar com.example.simplemaps.SimpleMaps
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.layout.HBox.getChildren()" because "this.buttonBox" is null
    at com.example.simplemaps.View.addWhereToButtons(View.java:66)
    at com.example.simplemaps.Controller.start(Controller.java:21)
    at com.example.simplemaps.SimpleMaps.start(SimpleMaps.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application com.example.simplemaps.SimpleMaps

Process finished with exit code 1

You need to cast a new HBox to buttonBox, as such.因此,您需要将新的 HBox 转换为 buttonBox。

HBox buttonBox = new HBox();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM