2017-04-18 97 views
1

早上好!我刚开始使用Java FX和Java FX FXML。我使用Scene Builder来构建GUI和Netbeans来编写剩余的Java FX程序。onAction =“#ClickedClickMe”不能在JavaFX FXML中工作

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

错误,我在FXML文件看到:
控制器未在根组件定义。

异常文本:

执行sample1.jar使用平台的C:\ PROGRAM 文件\的Java \ jdk1.8.0_121 \在应用程序的JRE /斌/ Java异常启动 方法java.lang。 reflect.InvocationTargetException处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at 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) 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:未指定控制器。 文件:/ 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)at javafx.fxml.FXMLLoader。负载(FXMLLoader.java:3097)在 sample1.Sample1.start(Sample1.java:22)在 com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863) 在 融为一体。 sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda为$ null $ 173(PlatformImpl.java:295) 在java.security.AccessController在 com.sun.javafx.application.PlatformImpl.lambda $ runLater .doPrivileged(本机方法)$ 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并添加任何其他窗格和按钮,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 
    }  

} 

回答

0

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

当我继续发布这个问题后浏览的解决方案,发现了这个链接。 http://javafxforbeginners.blogspot.in/2014/02/solved-error-controller-is-not-defined.html 你的答案完全一样。谢谢!现在问题解决了。 – Harish