JavaFX中使用Gradle时需要位置 [英] Location is required in JavaFX with gradle

查看:126
本文介绍了JavaFX中使用Gradle时需要位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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



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



build.gradle

 < code apply 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示例应用程序'
mainClass'local.hz.FXMLExample'
}

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

fxml_example.fxml

 <?xml version =1.0encoding =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/fxmlalignment =centerhgap =10 vgap =10>
< padding>< / inset top =25right =25bottom =10left =25/>< / padding>
< gridLinesVisible> true< / gridLinesVisible>

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

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

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

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

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

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

< Text fx:id =actiontarget
GridPane.columnIndex =1GridPane.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));

场景场景=新场景(root,300,275);

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


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


$ / code $ / pre

解决方案

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



尝试将其添加到您的build.gradle文件中:

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


I'm getting

java.lang.NullPointerException: Location is required.

when I run my program after assembling using gradle with javafx plugin. If I run it from IntelliJ Idea, everything is all right. The Java source files and .fxml are locate in some package.

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);
}
}

解决方案

Your FXML files are probably not packaged into your jar.

Try adding this to your build.gradle file:

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

这篇关于JavaFX中使用Gradle时需要位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆