如何在其他类中使用带有FXML文档创建的AnchorPane?

时间:2019-06-09 15:29:59

标签: java javafx

我的程序代码有问题! 我想使用由FXML文档创建的AnchorPane。 AnchorPane将由控制器使用initialize方法初始化!

这是我的程序的一些代码,其中包含错误:

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class MenuFXMLController implements Initializable{

    @FXML
    public AnchorPane INSTALLED_PANE, DOWNLOAD_PANE;

    public static AnchorPane STATIC_PANE_INSTALLED, STATIC_PANE_DOWNLOAD;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        MenuFXMLController.STATIC_PANE_INSTALLED = INSTALLED_PANE;
        MenuFXMLController.STATIC_PANE_DOWNLOAD = DOWNLOAD_PANE;


        System.out.println("Menu Controller initialized!");
    }

}
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javafx.embed.swing.SwingFXUtils;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import schnartzke.luca.engine.gui.controller.MenuFXMLController;

public class AddonShowcase {

    public static ArrayList<AddonShowcase> addonshowcaseList = new ArrayList<>();

    private String name;
    private BufferedImage img;

    public AddonShowcase(String name, BufferedImage img){
        this.name = name;
        this.img = img;
    }

    public Pane getShowCasePane(){
        try {
            Pane pa = new Pane();
            pa.setPrefSize(100, 80);
            Image image = SwingFXUtils.toFXImage(img, null);
            ImageView iv = new ImageView(image);
            iv.setFitWidth(100);
            iv.setFitWidth(40);
            iv.setX(0);
            iv.setY(20);
            pa.getChildren().add(iv);
            Text txt = new Text(name);
            txt.setX(0);
            txt.setY(0);
            txt.setFont(new Font("Arial", 12));
            pa.getChildren().add(txt);
            return pa;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void printAddonShowcaseListToDownloadAddonsPane(){
        AnchorPane ap = MenuFXMLController.STATIC_PANE_DOWNLOAD;

        for(int i = 0; i < addonshowcaseList.size(); i++){
            Pane p = addonshowcaseList.get(i).getShowCasePane();

            p.setLayoutX(i * 110);
            Text txt = new Text(100, 100, "Test Text fick mich AMK");
            txt.setFont(new Font("arial", 20));
            //ap.getChildren().add(p);
            ap.getChildren().add(txt);
        }

    }

}

堆栈跟踪:

java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-2-thread-1
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at package.AddonShowcase.printAddonShowcaseListToDownloadAddonsPane(AddonShowcase.java:63)
    at package.AuthentifikationSystem$InputCheckService.run(AuthentifikationSystem.java:216)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案