onAction="#ClickedClickMe" 在 JavaFX FXML 中不起作用

Har*_*ish 2 javafx fxml

早上好!我刚刚开始学习 Java FX 和 Java FX FXML。我使用 Scene Builder 构建 GUI,并使用 Netbeans 来编写剩余的 Java FX 程序。

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

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

异常文本:

使用平台执行sample1.jar C:\Program Files\Java\jdk1.8.0_121\jre/bin/java Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 处的应用程序启动方法 java.lang.reflect.InvoiceTargetException 中出现异常.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 com.sun。 javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 在 com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect。 NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 sun.launcher.LauncherHelper$FXHelper .main(LauncherHelper.java:767) 引起:java.lang.RuntimeException: com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) com.sun.javafx.application 处的应用程序启动方法中出现异常.LauncherImpl.lambda$launchApplication$155(LauncherImpl.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

在 javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) 在 javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) 在 javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557) 在 javafx .fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599) 在 javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) 在 javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) 在 javafx. fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 在 javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:3175) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java: [第 3104 章]第863章.AccessController.doPrivileged(本机方法)位于 com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)位于 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95 ) 在 com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在 com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 更多异常运行应用程序sample1.Sample1选择了_JAVA_OPTIONS:-Djava.net.preferIPv4Stack = true J​​ava结果:1

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

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

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

我得到的最接近的是这个,所以任何帮助肯定会为我带来帮助! https://bugs.openjdk.java.net/browse/JDK-8091582

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

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>
Run Code Online (Sandbox Code Playgroud)

示例1.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);
    }

}
Run Code Online (Sandbox Code Playgroud)

FXMLDocumentController.java

/*
 * 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
    }    

}
Run Code Online (Sandbox Code Playgroud)

Key*_*eri 5

在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">
Run Code Online (Sandbox Code Playgroud)