无法使用嵌入JavaFX配置框架

时间:2015-12-06 16:52:58

标签: java swing javafx

我有一个带按钮的框架。当我点击它时,会创建一个用JFXPanel嵌入文本的新框架。当我关闭窗口并再次点击按钮时,JFXPanel(文本)不会显示在框架中。问题似乎只有在我设置默认关闭操作时 - DISPOSE。为什么会这样?我真的需要你的帮助。 以下是代码:

public class TestJavaFXInSwingAfterDisposing {

    public static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Swing and JavaFX");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosed(java.awt.event.WindowEvent evt) {
                Platform.setImplicitExit(false);
            }    
        });

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

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

    private static Scene createScene() {
        Group  root  =  new  Group();
        Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
        Text  text  =  new  Text();

        text.setX(40);
        text.setY(100);
        text.setFont(new Font(25));
        text.setText("Welcome JavaFX!");

        root.getChildren().add(text);

        return (scene);
    }    
}


public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        TestJavaFXInSwingAfterDisposing test = new TestJavaFXInSwingAfterDisposing();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                test.initAndShowGUI();
            }
        });       
    }                                        

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }                     
    private javax.swing.JButton jButton1;                 
}

1 个答案:

答案 0 :(得分:0)

JavaFX IllegalStateException when disposing JFXPanel in Swing

最后我找到了解决方案。此命令仅在以下情况下才有效 它在main方法()中调用。