无法使用JavaFX

时间:2015-11-02 12:50:10

标签: java swing javafx

我有一个奇怪的问题,我已经面对几个小时,但我无法解决。我正在为无人机开发一个接口,我需要在我的javaFX场景中加载一个swingPanel。为此,我使用SwingNode。但无论我尝试什么,我似乎无法填补SwingNode。我没有得到任何错误,并且场景的其余部分正确加载。我做错了什么?

DroneScreenController,尝试填充SwingNode的类

public class DroneScreenController {

    // Model
    private DataBean dataBean;

    // View
    private DroneScreenView view;

    public SwingNode cameraNode;


    public DroneScreenController() {
        this.dataBean = Model.getInstance().getDataBean();
        this.view = new DroneScreenView(); //Not very interesting, it just loads the fxml file
    }

    public void show(){
        createSwingContent(cameraNode);
        try {
            view.show(dataBean.getPrimaryStage());
            dataBean.getPrimaryStage().setFullScreen(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createSwingContent(SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            //Just random swing Panel for testing purposes
                JPanel test = new JPanel(); 
                test.setSize(1000, 1000);
                test.setBackground(Color.red);
                swingNode.setContent(test);

            }
        });
    }

DroneScreenView,这个类只加载FXML文件

public class DroneScreenView {

    public DroneScreenView() {
    }

    public void show(Stage stage) throws IOException {
        stage.setTitle("PiStorm - DroneViewScreen");
        stage.setScene(new Scene( FXMLLoader.load(getClass().getResource("droneScreen.fxml"))));
        stage.show();
    }

}

FXML文件droneScreen.fxml

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

  <?import javafx.scene.shape.*?>
  <?import javafx.scene.image.*?>
  <?import javafx.embed.swing.*?>
  <?import java.lang.*?>
  <?import javafx.scene.layout.*?>

  <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="1080.0" minWidth="1920.0" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="computer.view.DroneScreenController">
     <children>
        <SwingNode fx:id="cameraNode" layoutX="245.0" layoutY="252.0" />
        <Circle fill="DODGERBLUE" layoutX="225.0" layoutY="100.0" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
        <ImageView fitHeight="1169.0" fitWidth="1920.0" layoutX="-6.0" layoutY="-8.0" pickOnBounds="true" preserveRatio="true">
           <image>
              <Image url="@Border%201920x1080.png" />
           </image>
        </ImageView>
     </children>
  </Pane>

0 个答案:

没有答案