的OnAction ="#ClickedClickMe"不能使用JavaFX FXML

时间:2017-04-18 06:29:04

标签: javafx fxml

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

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

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

例外文字:

  

使用平台C:\ Program执行sample1.jar   Files \ Java \ jdk1.8.0_121 \ jre / bin / java Application start中的异常   方法java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)     在   com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)   引起:java.lang.RuntimeException:Application start中的异常   方法   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)at   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)at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)at at   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)at at   javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)at at   sample1.Sample1.start(Sample1.java:22)at   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.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(Native Method)at   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 174(PlatformImpl.java:294)     在   com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at   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并添加任何其他窗格和Button,则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>

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 个答案:

答案 0 :(得分:2)

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