简体   繁体   English

使用JavaFX时无法在Kotlin中自己推断正确的类型

[英]Can't infer proper type myself in Kotlin when using JavaFX

I just started to study Kotlin and JavaFX by following tutorial. 我通过以下教程开始学习Kotlin和JavaFX。 I could see blank JavaFX windows, and I proceed next step that using FXML. 我可以看到空白的JavaFX窗口,然后继续使用FXML进行下一步。

import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.stage.Stage

class AppMain : Application() {

    override fun start(primaryStage: Stage) {
        primaryStage.title = "Try JavaFX"
        val fxml = javaClass.getResource("fxml/Main.fxml")
        val root = FXMLLoader.load(fxml) // ERRORS here! `load`
        val scene = Scene(root)

        primaryStage.scene = scene

        primaryStage.show()
    }

}

However, I couldn't figure out that how to avoid the type inferencing error like: 但是,我无法弄清楚如何避免类型推断错误,例如:
Error:(12, 31) Kotlin: Type inference failed: Not enough information to infer parameter T in fun <T : Any!> load(p0: URL!): T! Please specify it explicitly.

From the message, I understand that I have to write the type of variable fxml explicitly. 从该消息中,我了解到我必须显式地编写变量fxml的类型。 But I have no idea that what type should be labeled to fxml . 但是我不知道应该将什么类型标记为fxml

I tried to read the document about JavaFX, but I couldn't figure it out.(I'm not familiar with Java and Kotlin) I tried to type like URL but it does not make sense. 我试图阅读有关JavaFX的文档,但无法弄清楚。(我不熟悉Java和Kotlin)我试图键入类似URL但这没有任何意义。

Many JavaFX & Kotlin example codes that I could find from google does not seems to have the problem like this. 我可以从Google找到的许多JavaFX&Kotlin示例代码似乎没有这样的问题。 (Are the example codes written in previous version?) What type should I put for the variable? (示例代码是在以前的版本中编写的吗?)我应该为变量添加哪种类型? Or did I miss something other? 还是我想念其他东西?

Environment and Codes 环境与规范

Environment 环境

  • JDK 11 JDK 11
  • JavaFX 11 JavaFX 11
  • Kotlin 1.2.71 科特林1.2.71

My complete trial code 我完整的试用代码

https://github.com/QuietJoon/StudyKotlin-JavaFX/tree/fxml https://github.com/QuietJoon/StudyKotlin-JavaFX/tree/fxml

The problem isn't the parameter to the FXMLLoader.load function (which is a java.net.URL object, as returned by javaClass.getResource ). 问题不是FXMLLoader.load函数的参数(这是一个java.net.URL对象,由javaClass.getResource返回)。 It's that this function returns a generic type: 就是这个函数返回一个泛型类型:

public static <T> T load(URL location)

The Kotlin compiler needs to know what type your root variable will be (as you've not explicitly defined it), but it can't know that as there's nothing in the code that will allow it to infer this. Kotlin编译器需要知道您的root变量将是哪种类型(因为您尚未显式定义它),但是它不能知道,因为代码中没有任何内容可以推断出它。

A quick Google returned this example which has this code in it (in Java): 快速的Google返回了示例,其中包含以下代码(使用Java):

Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

As you can see here, the root variable is of type Parent . 如您所见, root变量的类型为Parent So what you need to do is provide this type (ie what you expect the load function to return) in some way. 因此,您需要做的是以某种方式提供这种类型(即您期望load函数返回的内容)。 Here are two different ways you could do this: 您可以通过两种不同的方法执行此操作:

  1. Specify the type explicitly when declaring the variable: val root: Parent = FXMLLoader.load(fxml) 声明变量时显式指定类型: val root: Parent = FXMLLoader.load(fxml)
  2. Specify the generic type when calling the method: val root = FXMLLoader.load<Parent>(fxml) 调用方法时,请指定通用类型: val root = FXMLLoader.load<Parent>(fxml)

Note also that in your build.gradle file in your github repo there's a mistake that means the code didn't immediately compile when I fetched it: 还要注意,在github build.gradle中的build.gradle文件中,有一个错误,这意味着在我获取代码时,代码没有立即编译:

compile "org.openjfx.javafx.fxml:11:$platform" should be compile "org.openjfx:javafx-fxml:11:$platform" (one of the dots should be a colon). compile "org.openjfx.javafx.fxml:11:$platform"compile "org.openjfx:javafx-fxml:11:$platform" (点之一应为冒号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM