gpt4 book ai didi

Swing 中的 JavaFX 集成

转载 作者:行者123 更新时间:2023-12-01 19:53:39 25 4
gpt4 key购买 nike

我是 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>

这是我的 Controller 类:

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

我理解错误消息,但不知道如何解决。

提前谢谢您。

最佳答案

我不确定您想要实现什么目标。发布的代码无法按发布的方式运行,因此我创建了一个快速而肮脏的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 更改为不需要 Controller 的 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>

关于Swing 中的 JavaFX 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com