简体   繁体   English

如何将 fxml 创建的 javafx GUI 与硬编码创建的 javafx gui 合并?

[英]How to incorporate a javafx GUI create by fxml with a javafx gui created by hardcode?

Is it poosible to create a button on the javafx fxml using screen builder such that the button would run an javafx file where its GUI was created via coding.是否可以使用屏幕构建器在 javafx fxml 上创建一个按钮,以便该按钮运行一个 javafx 文件,其中通过编码创建其 GUI。

FXML文件格式

public void start(Stage primaryStage) {
    try {
        pane.getChildren().setAll(heatMap.getHeatMapImage());
        heatMap.addEvents(events);
        AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("GUI2.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static void main(String[] args) throws NumberFormatException, IOException {
    // BasicConfigurator.configure();

    launch(args);
}

JAVAFX GUI CREATED BY CODE由代码创建的 JAVAFX GUI

@Override
public void start(Stage stage) throws Exception {
    pane.getChildren().setAll(heatMap.getHeatMapImage());
    HBox hbox = new HBox(pane); 

    // set spacing 
    hbox.setSpacing(10); 
    // set alignment for the HBox 
    hbox.setAlignment(Pos.CENTER); 

    // create a scene 
    Scene Overlay = new Scene(hbox, 1000, 600); 
    FileInputStream input = new FileInputStream("C:\\Users\\User\\Downloads\\map.jpeg");
    Image image = new Image(input);
    BackgroundImage backgroundimage = new BackgroundImage(image,  
                                        BackgroundRepeat.NO_REPEAT,  
                                        BackgroundRepeat.NO_REPEAT,  
                                        BackgroundPosition.DEFAULT,  
                                           BackgroundSize.DEFAULT); 
    Background background = new Background(backgroundimage);
    hbox.setBackground(background);

    stage.setTitle("JavaFX HeatMap Demo");
    stage.setScene(Overlay);
    stage.show();
    heatMap.addEvents(events);

}

public static void main(String[] args) {
    Dimension2D mapDimension = new Dimension2D(1500, 1577);
    Point2D     upperLeft    = new Point2D(0, 9.8);
    Point2D     lowerRight   = new Point2D(53.45, 10.2);
    Point2D     location     = new Point2D(53.7, 9.95);

    //System.out.println(Helper.latLongToPixel(mapDimension, upperLeft, lowerRight, location));

    launch(args);
}

So my question would be how do I call my JAVAFX GUI created by code from the fxml file ?所以我的问题是如何调用由 fxml 文件中的代码创建的 JAVAFX GUI?

Define the GUI in an fxml file and specify its controller:fxml文件中定义 GUI 并指定其控制器:

GUI2.fxml GUI2.fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="root" prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/10.0.1" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="fx_tests.GUI2Controller" />

Do the initialization in the controller.在控制器中进行初始化。
Add getters for information that you want to retrieve from the controller later:为稍后要从控制器检索的信息添加 getter:

import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.layout.StackPane;

public class GUI2Controller{

    @FXML StackPane root;
    private Point2D[] events;
    private SimpleHeatMap heatMap;

    @FXML
    public void initialize() {

        //initialize here all info that you want to retrieve later
        heatMap = new SimpleHeatMap(400, 400, ColorMapping.LIME_YELLOW_RED, 40);
        events  = new Point2D[] {
                new Point2D(110, 238),
                new Point2D(120, 144),
                new Point2D(207, 119),
                new Point2D(315, 348),
                new Point2D(264, 226),
                new Point2D(280, 159),
                new Point2D(240, 186),
                new Point2D(228, 170),
                new Point2D(234, 160),
                new Point2D(214, 170),
                new Point2D(200, 200),
        };

        //you could also populate root
        //root.getChildren().setAll(heatMap.getHeatMapImage());
    }

    public SimpleHeatMap getHeatMap() {
        return heatMap;
    }

    public  Point2D[] getEvents() {
        return events;
    }
}

Use it in the application:在应用程序中使用它:

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class FxmlTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GUI2.fxml"));
            Pane root = fxmlLoader.load();
            GUI2Controller controller = fxmlLoader.getController(); 

            //retrieve info from controller 
            SimpleHeatMap heatMap = controller.getHeatMap();
            Point2D[] events = controller.getEvents();

            //todo: use as needed 
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

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

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

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