【发布时间】:2018-10-26 18:14:38
【问题描述】:
我是使用 JavaFX 的新手,我在弄清楚如何在我的 swing 应用程序中集成 fxml 文件时遇到了一些问题。 在线阅读我找到了一种加载 fxml 文件的方法,并按照本指南将其显示在屏幕上: Integrating JavaFX into Swing Applications
我的代码应该做的是加载我已经开始使用 JavaFX Scene Builder 设计的 fxml 文件,将 JFXPanel 添加到 JPanel,并通过方法调用返回它。然后将其添加到主应用程序中的 tabbedPanel(因此可能同时打开多个不同的 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
我理解错误信息,但我不知道如何解决它。
提前谢谢你。
【问题讨论】:
-
根据我的经验,最好不要浪费时间。如果 Swing 应用程序很大,只需继续使用 Swing。如果 Swing 应用程序不大,只需将其迁移到 JavaFX 即可。你确实分开了你的担忧,不是吗? ;)
-
是的,这是一个很大的应用程序。但我认为我可以为此改进界面。我需要做这样的事情:imgur.com/a/plWvr5d
标签: java swing javafx javafx-8 javafx-2