使用SceneBuilder 2.0创建的Java 8的FXML与NetBeans Gluon(插件)项目相结合?

时间:2015-07-20 00:22:42

标签: android javafx fxml netbeans-8 javafxports

我能够创建和运行JavaFX FXML" hello world"使用NetBeans的程序。我安装了Gluon插件,允许JavaFX程序使用相同的代码库部署到桌面,IOS和Android。我试图将两者结合起来(FXML)......以获得一个'#hello world"程序,其中GUI部分是使用SceneBuilder 2.0在FXML中创建的,并部署到桌面,Android和& IOS通过使用Gluon插件创建的项目构建的项目。

这里是主要方法的代码......

package com.troyfirstgluonfxapplication;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author tfrericks
 */
public class TroyFirstGluonFXApplication 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);
    }

}

这是来自控制器的代码......

package com.troyfirstgluonfxapplication;
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;

/**
 *
 * @author tfrericks
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

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

}

这是FXML ......     

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.troyfirstgluonfxapplication.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

我收到的错误是......

Executing: gradle run

:compileJava UP-TO-DATE
:compileRetrolambdaMain UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileAndroidJava SKIPPED
:compileRetrolambdaAndroid SKIPPED
:compileTestJava UP-TO-DATE
:compileRetrolambdaTest SKIPPED
:compileRetrolambda UP-TO-DATE
:compileDesktopJava UP-TO-DATE
:processDesktopResources UP-TO-DATE
:desktopClasses UP-TO-DATE
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.ja va:363)
at  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at  com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at  com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl .java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1732398722.run(Unknown  Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at  com.troyfirstgluonfxapplication.TroyFirstGluonFXApplication.start(TroyFirstGluonFXApplication.java:23)
at  com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImp l.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1284648825.run(Unknown  Source)
at  com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:3 23)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1830989796.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291 )
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown  Source)
at 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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    ... 1 more
Exception running application  com.troyfirstgluonfxapplication.TroyFirstGluonFXApplication
:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_25\bin\java.exe'' finished    with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or -- debug option to get more log output.

BUILD FAILED

Total time: 2.29 secs



Build failure (see the Notifications window for stacktrace): gradle run

非常感谢任何帮助。

特洛伊。

1 个答案:

答案 0 :(得分:1)

该错误意味着它无法找到FXML文件。

在Projects视图的Resources [Main]包下,您应该创建包FXMLDocument.fxml并放置src |-- main | |-- java | | |-- com | | | |-- troyfirstgluonfxapplication | | | | |-- TroyFirstGluonFXApplication.java | | | | |-- FXMLDocumentController.java | |-- resources | | |-- com | | | |-- troyfirstgluonfxapplication | | | | |-- FXMLDocument.fxml 文件。

如果选中“文件”视图,则应该具有以下内容:

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

    Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
    Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

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

另请注意,您可以将fxml加载与插件为您生成的代码组合在一起:

{{1}}