onAction="#ClickedClickMe";不在JavaFXML中工作

onAction="#ClickedClickMe";不在JavaFXML中工作,javafx,fxml,Javafx,Fxml,早上好!我刚刚开始在JavaFX和JavaFXML上进行升级。我正在使用Scene Builder构建GUI和Netbeans,以编写剩余的Java FX程序 我面临的问题是在FXML文件中。Netbeans编辑器在onAction事件的.fxml文件中显示以下代码的错误 我在fxml文件中看到的错误: 未在根组件上定义控制器 异常文本: 使用平台C:\程序执行sample1.jar Files\Java\jdk1.8.0\u 121\jre/bin/Java应用程序启动时出现异常 方法java

早上好!我刚刚开始在JavaFX和JavaFXML上进行升级。我正在使用Scene Builder构建GUI和Netbeans,以编写剩余的Java FX程序

我面临的问题是在FXML文件中。Netbeans编辑器在onAction事件的.fxml文件中显示以下代码的错误

我在fxml文件中看到的错误:
未在根组件上定义控制器

异常文本:

使用平台C:\程序执行sample1.jar Files\Java\jdk1.8.0\u 121\jre/bin/Java应用程序启动时出现异常 方法java.lang.reflect.InvocationTargetException位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:498) com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389) 在 com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328) 位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:498) sun.launcher.launchelper$FXHelper.main(launchelper.java:767) 原因:java.lang.RuntimeException:应用程序启动时出现异常 方法在 com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917) 在 com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182) 在java.lang.Thread.run(Thread.java:745)处,由以下原因引起: javafx.fxml.LoadException:未指定控制器。 文件:/C:/Users/hmitty/Documents/NetBeansProjects/sample1/dist/run1317822864/sample1.jar/sample1/FXMLDocument.fxml:21

在 fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) 在javafx.fxml.fxmloader.access$100(fxmloader.java:103)上 fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557) 在 fxml.FXMLLoader$Element.ProcessEventHandlerAttribute(FXMLLoader.java:599) 在 FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) 在javafx.fxml.fxmloader.processEndElement(fxmloader.java:2823)的 javafx.fxml.fxmloader.loadImpl(fxmloader.java:2532)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:2441)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:3214)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:3175)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:3148)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:3124)位于 javafx.fxml.fxmloader.loadImpl(fxmloader.java:3104)位于 javafx.fxml.fxmloader.load(fxmloader.java:3097)位于 sample1.sample1.start(sample1.java:22)位于 com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.java:863) 在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 在 com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 又出现1个运行应用程序sample1.sample1的异常 _JAVA_选项:-Djava.net.preferIPv4Stack=true JAVA结果:1

如果我使用Netbeans创建一个示例项目,它使用AnchorPane,一切看起来都很好。但是,如果我删除AnchorPane并添加任何其他窗格和按钮,onAction事件将不起作用

我是这门语言的初学者,因此非常感谢任何帮助/指导

注意:我花了将近半天的时间在StackOverFlow上尽可能地浏览所有可用的解决方案

我得到的最接近的是这个,所以任何帮助都会帮我移动东西!

所有代码都粘贴在下面。如果还需要什么,请告诉我

FXML代码:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.event.ActionEvent?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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>
   <children>
      <Button mnemonicParsing="false" onAction="#ClickedClickMe" text="Click Me!" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Label fx:id="label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
   </children>
</GridPane>
FXMLDocumentController.java

package sample1;

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

public class Sample1 extends Application {

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

        Scene scene = new Scene(root);

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

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

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sample1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void ClickedClickMe(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Clicked Try me!!");
    }

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

}

在fxml文件中设置控制器

<GridPane fx:controller="sample1.FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">

发布此问题后,当我继续浏览解决方案时,发现了此链接。你的答案完全一样。谢谢问题现在解决了。