简体   繁体   English

JavaFX:使用注释创建的构造函数

[英]JavaFX: Constructor created with annotation

I am creating a JavaFX application and when I create a controller for my FXML file the constructors are always the same. 我正在创建JavaFX应用程序,当我为FXML文件创建控制器时,构造函数始终相同。

Is there any way to write a custom annotation to create my constructors? 有什么方法可以编写自定义注释来创建我的构造函数? Something like this: 像这样:

public class MyClass() {

    @InitFxml(file = "test")
    public MyClass() {
    }

And the @InitFxml would inject the following code into the constructor: @InitFxml会将以下代码注入到构造函数中:

FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml");
...

or is it possible to create annotation for the class which creates this default constructor? 还是可以为创建此默认构造函数的类创建注释?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

To process the annotation, you would have to define some kind of container that processed it, and always load your class through that container, or define an annotation processor which you attached to the compiler (I think: I know nothing about that second option). 要处理注释,您必须定义某种处理该注释的容器,并始终通过该容器加载类,或者定义附加到编译器的注释处理器(我认为:我对第二种选择一无所知) 。

Why not just pass a string as a parameter, though. 但是,为什么不直接将字符串作为参数传递呢? You could define an interface: 您可以定义一个接口:

import java.net.URL;

import javafx.fxml.FXMLLoader;


public interface CustomComponent {
    public default void loadFXML(String fxml) {
        try {
            URL resource = getClass().getResource(fxml);
            FXMLLoader loader = new FXMLLoader(resource);
            loader.setRoot(this);
            loader.setController(this);
            loader.load();
        } catch (Exception exc) {
            if (! (exc instanceof RuntimeException)) {
                throw new RuntimeException(exc);
            } else {
                throw (RuntimeException)exc ;
            }
        }
    }
}

and then just have your custom components implement it, calling the method from the constructor: 然后让您的自定义组件实现它,并从构造函数中调用方法:

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CustomComponentTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(new CustomVBox(), 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static class CustomVBox extends VBox implements CustomComponent {

        @FXML
        private Label label ;

        public CustomVBox() {
            loadFXML("CustomVBox.fxml");
        }

        @FXML
        private void click() {
            System.out.println("Click!");
        }
    }

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

This seems to be no heavier than defining an annotation on an empty constructor. 这似乎比在空构造函数上定义注释重得多。

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

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