Using JavaFX controller without FXML

后端 未结 3 788
囚心锁ツ
囚心锁ツ 2020-12-08 18:01

Is there a possibility to use a controller with a JavaFX GUI without using FXML.

I noticed that the FXML file contains an fx-controller attribute to bin

3条回答
  •  余生分开走
    2020-12-08 18:38

    I use JavaFX extensively and do not use FXML or scenebuilder. So I can vouch that it can be done.

    Below is the auto generated code made by my IDE to get an JavaFX main class. This will be the root of your application. You will then add to it to create your application.

    public class NewFXMain extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler() {
    
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
    
        StackPane root = new StackPane();
        root.getChildren().add(btn);
    
        Scene scene = new Scene(root, 300, 250);
    
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    }
    

提交回复
热议问题