TornadoFX 没有找到 JavaFx
TornadoFX not finding JavaFx
我正在使用 TornadoFx 尝试我的第一个应用程序,所以我从以下代码开始:
package no.tornado.fxsample.workspace
import javafx.application.Application
import tornadofx.*
fun main(args: Array<String>) = launch<MyApp>(args)
class MyApp: App(MyView::class)
class MyView: View() {
override val root = VBox()
init {
with(root) {
this += Button("Press Me")
this += Label("Waiting")
}
}
}
但显然它充满了错误,并且无法找到 JavaFX
我的gradle.build
是:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MyAppKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
也许您使用的是 OpenJDK,它默认没有 JavaFX?我强烈建议您安装 Oracle JDK 8.
您的代码使用了一些严重过时的语法。由于您是新用户,我怀疑我们周围有一些过时的代码示例 - 您能告诉我您是在哪里找到这些示例的吗?
现在应该这样写:
class MyView : View() {
override val root = vbox {
button("Press me")
label("Waiting")
}
}
这是另一种选择,我只是将其写为答案,以防有人对此感兴趣。
下面的代码现在 运行 我使用 command line
和 MS VS Code
:
main.kt
:
import javafx.*
import javafx.application.Application
import javafx.scene.control.Button
import javafx.scene.layout.VBox
import tornadofx.*
import javafx.scene.control.Label
class MyApp: App(MyView::class)
class MyView : View() {
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
Application.launch(MyApp::class.java, *args)
}
build.gradle
:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MainKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
输出为:
但是在 Intellij IDEA 中仍然报错如下:
参考this 它通过添加这个插件与 Intellij Idea 一起工作:
apply from: "http://dl.bintray.com/content/shemnon/javafx-gradle/8.1.1/javafx.plugin"
不幸的是这个插件是 not maintained any more :(
也比较奇怪,这个代码是不是只需要一次,之后就可以删除了,没有任何错误,可能是在什么地方兑现了!!
ver gradle 6.4
版本 kotlin 1.3.72
版本 tornadofx 1.7.20
ver JDK 14
build.gradle 添加
plugins {
id 'org.openjfx.javafxplugin' version '0.0.8'
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
我正在使用 TornadoFx 尝试我的第一个应用程序,所以我从以下代码开始:
package no.tornado.fxsample.workspace
import javafx.application.Application
import tornadofx.*
fun main(args: Array<String>) = launch<MyApp>(args)
class MyApp: App(MyView::class)
class MyView: View() {
override val root = VBox()
init {
with(root) {
this += Button("Press Me")
this += Label("Waiting")
}
}
}
但显然它充满了错误,并且无法找到 JavaFX
我的gradle.build
是:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MyAppKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
也许您使用的是 OpenJDK,它默认没有 JavaFX?我强烈建议您安装 Oracle JDK 8.
您的代码使用了一些严重过时的语法。由于您是新用户,我怀疑我们周围有一些过时的代码示例 - 您能告诉我您是在哪里找到这些示例的吗?
现在应该这样写:
class MyView : View() {
override val root = vbox {
button("Press me")
label("Waiting")
}
}
这是另一种选择,我只是将其写为答案,以防有人对此感兴趣。
下面的代码现在 运行 我使用 command line
和 MS VS Code
:
main.kt
:
import javafx.*
import javafx.application.Application
import javafx.scene.control.Button
import javafx.scene.layout.VBox
import tornadofx.*
import javafx.scene.control.Label
class MyApp: App(MyView::class)
class MyView : View() {
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
Application.launch(MyApp::class.java, *args)
}
build.gradle
:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MainKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
输出为:
但是在 Intellij IDEA 中仍然报错如下:
参考this 它通过添加这个插件与 Intellij Idea 一起工作:
apply from: "http://dl.bintray.com/content/shemnon/javafx-gradle/8.1.1/javafx.plugin"
不幸的是这个插件是 not maintained any more :(
也比较奇怪,这个代码是不是只需要一次,之后就可以删除了,没有任何错误,可能是在什么地方兑现了!!
ver gradle 6.4 版本 kotlin 1.3.72 版本 tornadofx 1.7.20 ver JDK 14
build.gradle 添加
plugins {
id 'org.openjfx.javafxplugin' version '0.0.8'
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}