有 Java 编程相关的问题?

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

使用java 11的windows JFXPanel设置

我想将openjfx集成到我的Java11代码中。在Windows 10上使用IntelliJ IDEA 2018.2.6,我创建了一个测试项目,并试用了以下代码

import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.TabPane;

public class Java11FXTestApplication {



    public static void main(String[] args) {
        JFXPanel dummyPanel;
        TabPane dummyTabPane;
        Scene dummyScene;
        System.out.println("Creating JFX Panel");
        dummyPanel = new JFXPanel();
        System.out.println("Creating  TabPane");
        dummyTabPane = new TabPane();
        System.out.println("Creating  Scene");
        dummyScene = new Scene(dummyTabPane);
        System.out.println("Setting  Scene");
        dummyPanel.setScene(dummyScene); //Freezing here
        System.out.println("Scene Created");
    }
}

此代码在setScene()方法调用中冻结。 我试着调试它,发现代码在secondaryLoop中无限期地等待。在JFXPanel中输入()调用。设置新方法。知道为什么吗

This code works fine in JDK-8 but not working with java-11.0.1.

我在这个问题上一无所获,有点被java11javafx问题所困扰。代码有问题吗?或javafx for java11的任何报告问题


共 (1) 个答案

  1. # 1 楼答案

    您正在主线程上设置Scene。从^{}emphasismine)的文档中:

    There are some restrictions related to JFXPanel. As a Swing component, it should only be accessed from the event dispatch thread, except the setScene(javafx.scene.Scene) method, which can be called either on the event dispatch thread or on the JavaFX application thread.

    Platform.runLater调用中包装setScene(或SwingUtilities.invokeLater

    Platform.runLater(() -> {
        dummyPanel.setScene(dummyScene);
        System.out.println("Scene Created");
    });
    

    请注意,对于当前代码,一旦main返回,JVM将继续运行。创建JFXPanel会初始化JavaFX运行时,直到最后一个窗口关闭(仅当Platform.isImplicitExittrue或调用Platform.exit时才会退出。由于您的代码两者都没有,JavaFX运行时将继续运行


    {}的文档还提供了一个如何使用它的示例(请注意,几乎所有事情都发生在事件调度线程JavaFX应用程序线程上):

    Here is a typical pattern how JFXPanel can used:

     public class Test {
    
         private static void initAndShowGUI() {
             // This method is invoked on Swing thread
             JFrame frame = new JFrame("FX");
             final JFXPanel fxPanel = new JFXPanel();
             frame.add(fxPanel);
             frame.setVisible(true);
    
             Platform.runLater(new Runnable() {
                 @Override
                 public void run() {
                     initFX(fxPanel);
                 }
             });
         }
    
         private static void initFX(JFXPanel fxPanel) {
             // This method is invoked on JavaFX thread
             Scene scene = createScene();
             fxPanel.setScene(scene);
         }
    
         public static void main(String[] args) {
             SwingUtilities.invokeLater(new Runnable() {
                 @Override
                 public void run() {
                     initAndShowGUI();
                 }
             });
         }
     }