有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Swing中的JavaFX集成

我刚开始使用JavaFX,在我的swing应用程序中如何集成fxml文件时遇到了一些问题。 通过联机阅读,我找到了加载fxml文件的方法,并按照以下指南将其显示在屏幕上: Integrating JavaFX into Swing Applications

我的代码应该做的是加载fxml文件,我已经开始使用JavaFX场景生成器设计该文件,将JFXPanel添加到JPanel中,并通过方法调用返回它。然后将其添加到主应用程序中的选项卡面板中(因此可能会同时打开多个不同的JFXPanel)

我已经解决了一些问题,但我不确定如何继续。我可以运行一个面板,但在我发布的教程中,使用了静态方法,因此我无法拥有多个不同的面板。我做了一些改变,但现在我完全被卡住了

这是我当前的FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.fcmModeler.fcmPanel">
   <children>
      <AnchorPane prefHeight="25.0" prefWidth="1000.0">
         <children>
            <Button fx:id="addComponent" layoutX="161.0" mnemonicParsing="false" onMouseClicked="#addComponent" prefHeight="25.0" prefWidth="600.0" text="+ Add Component" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
      </AnchorPane>
      <SplitPane dividerPositions="0.1713426853707415" layoutY="25.0" prefHeight="577.0" prefWidth="1000.0">
        <items>
          <AnchorPane fx:id="leftPane" maxWidth="160.0" minHeight="0.0" minWidth="160.0" prefHeight="575.0" prefWidth="160.0" />
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="546.0" prefWidth="683.0" />
        </items>
      </SplitPane>
   </children>
</Pane>

这是我的控制器类:

package GUI.fcmModeler;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

import javax.swing.*;
import java.io.IOException;
import java.util.Random;

public class fcmPanel extends JPanel {

    private JPanel frame;
    private static Group root=new Group();
    private boolean firstRun=true;
    Scene scene=null;

    private void initAndShowGUI() {
        // This method is invoked on the EDT thread
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(1200, 1200);
        frame.setVisible(true);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });

        frame.add(fxPanel);
        frame.setVisible(true);
    }

    private void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private Scene createScene() {
        //if(firstRun){
            //root = new Group();
            if(scene==null) {
                scene = new Scene(root, Color.ALICEBLUE);
            }
            try {
                Node newLoadedPane = FXMLLoader.load(fcmPanel.class.getResource("/fxml/fcmPanel.fxml"));
                root.getChildren().add(newLoadedPane);
            } catch (IOException e) {
                System.out.print("the file doesn'e exist.\n");
                e.printStackTrace();
            }
        //}

        return (scene);
    }

    public JPanel returnPanel() {
        System.out.println("returnpanel");
//        if(frame==null){
//            frame = new JPanel();
//        }
        //initFcmPanel();
        return frame;
    }

    public fcmPanel(){
        frame = new JPanel();
        //root=g;
        initFcmPanel();
    }


    public void initFcmPanel() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }

    public void addComponent(){
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("added component2");
                Circle circle = new Circle(new Random().nextInt(50),Color.BLUE);
                root.getChildren().add(circle);
                circle.relocate(new Random().nextInt(600),new Random().nextInt(600));
            }
        });

    }

}

JFXPanel通过以下调用添加到选项卡式面板:

tabbedPanel.addTab(newTabName , null,new fcmPanel().returnPanel(), filePath  );

就目前而言,我面临以下问题

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Group@3e0f3500[styleClass=root]is already set as root of another scene

我理解错误信息,但我不知道如何解决它

先谢谢你


共 (1) 个答案

  1. # 1 楼答案

    我不确定你想要实现什么。发布的代码不能以发布的方式运行,因此我创建了一个快速且脏的mcve版本的代码,该版本运行时没有错误,希望它能帮助您发现错误或澄清您的需求:

    import java.awt.Dimension;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javafx.application.Platform;
    import javafx.embed.swing.JFXPanel;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Group;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    
    public class FcmPanel {
    
        private static Group root=new Group();
        Scene scene=null;
    
        private void initAndShowGUI() {
    
            // This method is invoked on the EDT thread
            final JFXPanel fxPanel = new JFXPanel();
            fxPanel.setPreferredSize(new Dimension(600, 400));
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Platform.runLater(()-> initFX(fxPanel));
    
            JTabbedPane tabbedPanel = new JTabbedPane();
            tabbedPanel.addTab("Just an emty tab", new JPanel() );
            tabbedPanel.addTab("FcmPanel", fxPanel );
            frame.add(tabbedPanel);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void initFX(JFXPanel fxPanel) {
            Scene scene = createScene();
            fxPanel.setScene(scene);
        }
    
        private Scene createScene() {
    
            if(scene==null) {
                scene = new Scene(root, Color.ALICEBLUE);
            }
            try {
                Node newLoadedPane = FXMLLoader.load(getClass().
                        getResource("FcmPanel.fxml"));
                root.getChildren().add(newLoadedPane);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return (scene);
        }
    
        public static void main(String[] args) {
            FcmPanel p = new FcmPanel();
            p.initAndShowGUI();
        }
    } 
    

    这个fxml基本上是一样的。刚刚将Button更改为不需要控制器的Label

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.SplitPane?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.Pane?>
    
    <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" >
       <children>
          <AnchorPane prefHeight="25.0" prefWidth="1000.0">
             <children>
                <Label fx:id="addComponent" layoutX="161.0" mnemonicParsing="false"  prefHeight="25.0" prefWidth="600.0" text="+ Add Component" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
             </children>
          </AnchorPane>
          <SplitPane dividerPositions="0.1713426853707415" layoutY="25.0" prefHeight="577.0" prefWidth="1000.0">
            <items>
              <AnchorPane fx:id="leftPane" maxWidth="160.0" minHeight="0.0" minWidth="160.0" prefHeight="575.0" prefWidth="160.0" />
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="546.0" prefWidth="683.0" />
            </items>
          </SplitPane>
       </children>
    </Pane>