繁体   English   中英

使用Kotlin时,FXML控件始终为null

[英]FXML control always null when using Kotlin

使用IntelliJ我创建了一个JavaFX应用程序,然后将Kotlin和Maven作为框架添加到其中。 它附带了一个sample.fxml文件以及一个Controller.java和Main.java。 我在Kotlin(MainWindowController.kt)中为控制器创建了一个新类,并将sample.fxml文件重命名为MainWindow.fxml。 我将MainWindow.fxml更新为:

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<GridPane fx:controller="reader.MainWindowController" xmlns:fx="http://javafx.com/fxml" xmlns="http://javafx.com/javafx/8" alignment="center" hgap="10" vgap="10">
    <Label fx:id="helloLabel" text="Hello"/>
</GridPane>

在我的MainWindowController.kt文件中,我有:

package reader

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

class MainWindowController {

    @FXML var helloLabel: Label? = null

    init {
        println("Label is null? ${helloLabel == null}")
    }
}

这是我的Main.java:

import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("MainWindow.fxml"));
        primaryStage.setTitle("My App");
        primaryStage.setScene(new Scene(root, 1000, 600));
        primaryStage.show();
    }

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

当我运行应用程序时,打印行显示标签为空,否则窗口显示正确,我看到我的标签中的文本。 null是我遇到的问题。 我在Kotlin上使用FXML并没有太多发现,我发现它有点过时,似乎没有一个实际的工作解决方案。

有谁知道标签为什么是空的? 我一定是做错了什么或误解了什么。

编辑:由于快速回复,以下是我现有的工作:

package reader

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

class MainWindowController {

    @FXML var helloLabel: Label? = null

    fun initialize() {
        println("Label is null? ${helloLabel == null}")
    }
}

正如之前所提。 检查是否设置了fx:id。

也可以使用lateinit修饰符。

您的代码可能如下所示:

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

class MainWindowController {
    @FXML 
    lateinit var helloLabel : Label
}

就像使用Java构造函数一样, fx:id字段将不会被填充,但是 init (或Java构造函数)被调用之后。 一个常见的解决方案是实现Initializable接口(或者只是定义一个initialize()方法)并在方法中进行额外的设置,如下所示:

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

class MainWindowController : Initializable {
    @FXML 
    var helloLabel: Label? = null

    override fun initialize(location: URL?, resources: ResourceBundle?) {
        println("Label is null? ${helloLabel == null}")
    }
}

暂无
暂无

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

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