CalendarFX与JavaFX结合使用:未找到模块javafx.controls

时间:2020-03-21 18:41:21

标签: java gradle javafx

我试图在CalendarFX的JavaFX项目中使用gradle javafx plugin作为gradle依赖项,但是却收到错误,发现模块javafx.controls被发现,同时在构建中明确指定。 gradle文件,应使用javafx.controls模块。我的设置是这样:

build.gradle:

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

group 'org.example'
version '1.0-SNAPSHOT'

mainClassName = "org.example.MainApp"

sourceCompatibility = 13

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.calendarfx:view:11.8.3'
}

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

MainApp.java:

package org.example;

import com.calendarfx.model.Calendar;
import com.calendarfx.model.CalendarSource;
import com.calendarfx.view.CalendarView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.time.LocalDate;
import java.time.LocalTime;

public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        CalendarView calendarView = new CalendarView();

        Calendar test = new Calendar("Test");
        test.setShortName("T");
        test.setStyle(Calendar.Style.STYLE1);

        CalendarSource familyCalendarSource = new CalendarSource("Source");
        familyCalendarSource.getCalendars().add(test);

        calendarView.getCalendarSources().setAll(familyCalendarSource);
        calendarView.setRequestedTime(LocalTime.now());

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(calendarView);

        Thread updateTimeThread = new Thread("Calendar: Update Time Thread") {
            @Override
            public void run() {
                while (true) {
                    Platform.runLater(() -> {
                        calendarView.setToday(LocalDate.now());
                        calendarView.setTime(LocalTime.now());
                    });

                    try {
                        // update every 10 seconds
                        sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        };

        updateTimeThread.setPriority(Thread.MIN_PRIORITY);
        updateTimeThread.setDaemon(true);
        updateTimeThread.start();

        Scene scene = new Scene(stackPane);
        primaryStage.setTitle("Calendar");
        primaryStage.setScene(scene);
        primaryStage.setWidth(1300);
        primaryStage.setHeight(1000);
        primaryStage.centerOnScreen();
        primaryStage.show();
    }
}

当我尝试运行时遇到的错误是:

$ ./gradlew run

> Task :run FAILED
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home/bin/java'' 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. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 738ms
3 actionable tasks: 2 executed, 1 up-to-date

2 个答案:

答案 0 :(得分:1)

在您的VM参数中尝试以下操作:

--module-path \path\to\javafx-sdk\lib --add-modules=javafx.controls,javafx.fxml

答案 1 :(得分:1)

JavaFx不附带CalendarFx, 下载该库并将其导入您的项目中。

https://github.com/dlsc-software-consulting-gmbh/CalendarFX/releases/download/11.8.3/view-11.8.3.jar