【问题标题】:Running a simple javafx application with gradle project in eclipse在 Eclipse 中使用 gradle 项目运行一个简单的 javafx 应用程序
【发布时间】:2020-03-27 05:42:23
【问题描述】:

我是 Eclipse java、Gradle 和 Javafx 的新手。最近我正在尝试用 gradle 项目编写一个 javafx 应用程序。以下是我想提供的各种细节。

Javafx SDK version and location

Scene Builder version and location

我的问题在这里也引起了,填充实际上是如何在 Eclipse 中工作的?我应该在哪里放置驱动程序类?在 src/main/java 下?

My Eclipse directory

这就是我格式化我的用户界面的方式。

ui.fxml:

> <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171">
   <children>
      <Button fx:id="button1" layoutX="-317.0" layoutY="-145.0" mnemonicParsing="false" onAction="#buttonPressed" text="Hit me" />
      <Label fx:id="label1" layoutX="66.0" layoutY="-98.0" text="hi" />
   </children>
</AnchorPane>

build.gradle

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

repositories {
    mavenCentral()
}

dependencies {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
    version = "11.0.2"
}

mainClassName = 'Library'

我怀疑这里有一些问题,因为当我运行 gradle 项目时,我无法运行它。我只能构建它。

Gradle error: Cannot set the value of read-only property 'modules' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Library.java:(驱动类)

public class Library {
    public static void main(String arg[]) {
        ex2.UIApplication.run(arg);
    }
}

当我运行它时,我遇到了很多错误。

VM argument: --module-path "C:\Users\andes\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml

Error: Could not find or load main class Lab_3_1.Library

Lab3Controller.java:

package ex2;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class Lab3Controller {

    @FXML
    private Button button1;

    @FXML
    private Label label1;

    @FXML
    void buttonPressed(ActionEvent event) {

    }

}

UIApplication.java:

package ex2;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class UIApplication extends Application{
    @Override
    public void start(Stage stage) throws Exception{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/ui.fxml"));
        VBox root = (VBox) loader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Lab 3");;
        stage.show();
    }

    public static void run (String arg[]) {
        Application.launch(arg);
    }
}

【问题讨论】:

    标签: java eclipse gradle javafx


    【解决方案1】:

    问题是,您的 Mainclass 直接从 Application 扩展而来。

    您需要通过不从应用程序扩展的类来启动它。

    从这里开始。

    
    public class AppLauncher {
    
        public static void main(String[] args) {
            Application.launch(UIApplication.class, args);
        }
    }
    
    

    还有另一个提示。 如果您想在没有“vm Arguments”的情况下运行您的应用程序,您应该考虑使用 module-info.java 来告诉您的应用程序使用哪些模块。

    或者您可以更新您的 gradle 配置,因为您已经使用 Java fx 插件:

    
    javafx {
    
        version = "11"
        modules = [ 'javafx.controls', 'javafx.fxml','javafx.graphics']
    }
    
    dependencies {
    
        // your other dependecies
    
    
    
        implementation 'org.openjfx:javafx:11'
        compile group: 'org.openjfx', name: 'javafx', version: '11.0.2'
    
    
    
    }
    
    
    compileJava {
        doFirst {
            options.compilerArgs = [
                    '--module-path', classpath.asPath,
                    '--add-modules', 'javafx.controls,javafx.fxml,javafx.graphics'
            ]
            println options.compilerArgs
        }
    
    }
    
    
    
    

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2020-05-21
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多