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

Java 8's FXML created with SceneBuilder 2.0 combined with a NetBeans Gluon (plugin) project?

我能够使用 NetBeans 创建和 运行 JavaFX FXML "hello world" 程序。我安装了 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$launchApplication7(LauncherImpl .java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda/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$launchApplication13(LauncherImp l.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda/1284648825.run(Unknown  Source)
at  com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:3 23)
at com.sun.javafx.application.PlatformImpl$$Lambda/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda/1830989796.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291 )
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null1(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda/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

非常感谢任何帮助。

特洛伊

#

该错误表示找不到 FXML 文件。

在项目视图的资源 [Main] 包下,您应该创建包 com.troyfirstgluonfxapplication 并放置 FXMLDocument.fxml 文件。

如果您查看“文件”视图,您应该会看到类似这样的内容:

src
  |-- main
  |    |-- java
  |    |     |-- com
  |    |     |    |-- troyfirstgluonfxapplication
  |    |     |    |     |-- TroyFirstGluonFXApplication.java
  |    |     |    |     |-- FXMLDocumentController.java
  |    |-- resources
  |    |     |-- com
  |    |     |    |-- troyfirstgluonfxapplication
  |    |     |    |     |-- FXMLDocument.fxml     

另请注意,您可以将 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();
}