0

我能够使用 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$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

任何帮助是极大的赞赏。

特洛伊。

#

4

1 回答 1

2

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

在 Projects 视图的 Resources [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();
}
于 2015-07-20T08:17:02.517 回答