首页 文章

onAction =“#ClickedClickMe”无法在JavaFX FXML中使用

提问于
浏览
2

早上好!我刚刚开始使用Java FX和Java FX FXML . 我正在使用Scene Builder构建GUI和Netbeans来编写剩余的Java FX程序 .

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

Error that I see in fxml file
控制器未在根组件上定义 .

Exception Text:

使用平台C执行sample1.jar:\ Program Files \ Java \ jdk1.8.0_121 \ jre / bin / java在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)中的应用程序启动方法java.lang.reflect.InvocationTargetException中的异常sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at com.sun位于sun.reflect的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)的com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)中的.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) .nativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at 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:未指定控制器 . file:/ C:/Users/hmitty/Documents/NetBeansProjects/sample1/dist/run1317822864/sample1.jar!/sample1/FXMLDocument.fxml:21 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)at javafx . fxml.FXMLLoader.access $ 100(FXMLLoader.java:103)javafx.fxml.FXMLLoader $ Element.getControllerMethodHandle(FXMLLoader.java:557)位于javafx的favafx.fxml.FXMLLoader $ Element.processEventHandlerAttributes(FXMLLoader.java:599) . favml.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)at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)at javafx.fxml.FXMLLoader.load(FXMLLoader.ja va:3097)at sample1.Sample1.start(Sample1.java:22)at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863)at com.sun.javafx.application.PlatformImpl . lambda $ runAndWait $ 175(PlatformImpl.java:326)位于com.sun的java.security.AccessController.doPrivileged(Native Method)的com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295) . javafx.application.PlatformImpl.lambda $ runLater $ 174(PlatformImpl.java:294)at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)at com.sun.glass.ui.win.WinApplication com.sun.glass.ui.win.WinApplication.lambda上的._runLoop(Native方法)$ null $ 148(WinApplication.java:191)... 1更多异常运行的应用程序sample1.Sample1选择了_JAVA_OPTIONS:-Djava.net . preferIPv4Stack = true Java结果:1

如果我使用Netbeans创建一个示例项目,它使用AnchorPane,一切似乎都没问题 . 但是,如果我删除AnchorPane并添加任何其他窗格和Button,则onAction事件不起作用!

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

注意:我在StackOverFlow上尽可能地浏览所有可用的解决方案,做了近半天的功课 .

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

所有代码都粘贴在下面 . 如果需要其他任何东西,请告诉我!

FXML Code:

<?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>

Sample1.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);
    }

}

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
    }    

}

1 回答

  • 1

    Set controller in fxml file

    <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">
    

相关问题