JavaFX中需要使用gradle定位

时间:2014-01-15 03:34:10

标签: javafx gradle

我正在

  

java.lang.NullPointerException:位置是必需的。

当我使用gradle与javafx插件组装后运行程序时。如果我从IntelliJ Idea运行它,一切都很好。 Java源文件和.fxml位于某个包中。

的build.gradle

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
apply from: 'javafx.plugin'

javafx {
    javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45'

    appID 'FXMLExample'
    appName 'fxml example application'
    mainClass 'local.hz.FXMLExample'
}

task "create-dirs" {
    sourceSets*.java.srcDirs*.each {it.mkdirs()}
    sourceSets*.resources.srcDirs*.each {it.mkdirs()}
}

fxml_example.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane fx:controller="local.hz.FXMLExampleController"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
<gridLinesVisible>true</gridLinesVisible>

<Text text="Welcome"
      GridPane.columnIndex="0" GridPane.rowIndex="0"
      GridPane.columnSpan="2"/>

<Label text="User Name:"
      GridPane.columnIndex="0" GridPane.rowIndex="1"/>

<TextField
      GridPane.columnIndex="1" GridPane.rowIndex="1"/>

<Label text="Password:"
      GridPane.columnIndex="0" GridPane.rowIndex="2"/>

<PasswordField fx:id="passwordField"
               GridPane.columnIndex="1" GridPane.rowIndex="2"/>

<HBox spacing="10" alignment="bottom_right"
      GridPane.columnIndex="1" GridPane.rowIndex="4">
    <Button text="Sign In"
            onAction="#handleSubmitButtonAction"/>
</HBox>

<Text fx:id="actiontarget"
      GridPane.columnIndex="1" GridPane.rowIndex="6"/>
</GridPane>

FXMLExample.java

package local.hz;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLExample extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

    Scene scene = new Scene(root,300,275);

    stage.setTitle("FXML WELCOME!!");
    stage.setScene(scene);
    stage.show();
}

public static  void main(String[] arguments) {
    launch(arguments);
}
}

4 个答案:

答案 0 :(得分:20)

你的FXML文件可能没有打包到你的jar中。

尝试将此添加到build.gradle文件中:

sourceSets {
  main {
    resources {
        srcDirs = ["src/main/java"]
        includes = ["**/*.fxml"]
    }
  }
}

答案 1 :(得分:3)

我参加聚会真的很晚...但是被接受并没有用。我(随机地)将“ javafx.fxml”添加到模块列表中,就像这样

<input type="checkbox" @bind="State.ShowEasterEggs"/>Show Easter Eggs

@code {

    // Getting the Cascading Value as a parameter
    [CascadingParameter]
    protected State State { get; set; }

}

...它解决了问题。

答案 2 :(得分:0)

实际上,使用Gradle加载资源可能与使用IDE资源系统不同。

最好使用ClassLoader:

URL myFxmlURL = ClassLoader.getSystemResource("fxml_example.fxml");
FXMLLoader loader = new FXMLLoader(sampleUrl);

Parent root = loader.load(myFxmlURL);

答案 3 :(得分:-1)

尝试更换:

javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45'

使用:

javaRuntime = 'C:/Program Files/Java/jdk1.7.0_45'