java - Swing 组件内的 JFXPanel 将应用程序卡在 JFXPanel 构造上(os x 和 JDK 1.8)

标签 java macos swing javafx java-8

我正在 swing 组件 (JPanel) 内运行 JFXPanel。我遇到的问题是,有时(并非每次)应用程序在创建 JXFPanel 时挂起(卡住)。请参阅下面的代码。

public VideoPlayer(String url){
    if (MovieInfoConfig.DEBUG)
        System.out.println("1 Creating VideoPlayer Objct...");

    this.videoUrl = url;
    jfxPanel = new JFXPanel();

    if (MovieInfoConfig.DEBUG)
        System.out.println("2 JFXPanel object created...");

    createScene();
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(800, 560));
    add(jfxPanel, BorderLayout.CENTER);
}

您可以看到正在打印我的调试消息。我总是会到达第一步,但正如我所说,在某些情况下,程序在到达第二条调试消息之前会挂起。换句话说,jfxPanel = new JFXPanel() 行似乎导致了问题。

我只在 Mac OSX (Mavericks) JDK 1.8 上对此进行了测试。对我来说,这有点像 JavaFX/OSX JDK 1.8 错误 - 但我在网上没有找到任何有关它的信息。

有人知道吗?有什么方法可以让我调试 JFXPanel 构造函数本身,看看它在应用程序挂起之前发生了什么?

谢谢大家!

编辑 1 按照建议,我对我的主要方法做了一些更改。然而,它并没有消除问题。请参阅下面重现该问题的完整示例:

import java.awt.*;
import java.awt.event.*;
import javafx.application.*;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import javax.swing.*;

public class BrowserTest extends JFrame {
    JPanel videoP = new JPanel();
    BrowserTest() {
        super("Test");
        System.out.println("Start BT");
        setSize(1200, 700);     
        setLayout(new BorderLayout()); 
        JPanel p1 = new JPanel();
        String[] videos = new String[3]; 
        videos[0] = "https://www.youtube.com/embed/W-J2OYN9fF8?autoplay=true&controls=0";
        videos[1] = "https://www.youtube.com/embed/8hP9D6kZseM?autoplay=true&controls=0";
        videos[2] = "https://www.youtube.com/embed/Rq9eM4ZXRgs?autoplay=true&controls=0";
        for(int x = 0; x < videos.length; x++) {
            JButton b = new JButton("Video " + x); 
            b.addActionListener(new bClick(videos[x])); 
            p1.add(b);
        }
        add(p1, BorderLayout.NORTH);
        add(videoP, BorderLayout.CENTER); 
        setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 Platform.setImplicitExit(false);
                 new BrowserTest(); 
             }
         });
    }

    class bClick implements ActionListener {
        String url; 
        bClick(String url) {
            this.url = url; 
        }
        public void actionPerformed(ActionEvent e) {
            if (videoP.getComponents().length > 0) {    
                Component c = videoP.getComponent(0);
                if (c instanceof VideoPlayer)
                    ((VideoPlayer) c).stopTrailer(); 
            }
            videoP.removeAll();

            videoP.add(new VideoPlayer(url)); 
            System.out.println("Clicked url " + url); 
            videoP.revalidate();
            videoP.repaint();
        }
    }
}

class VideoPlayer extends JPanel {  
    private Stage stage;
    private WebView browser;
    private JFXPanel jfxPanel;
    private WebEngine webEngine;
    private String videoUrl;
    int xPos, yPos; 

    public VideoPlayer(String url){
        this.videoUrl = url;    
        System.out.println("1 Creating VideoPlayer Objct...");
        jfxPanel = new JFXPanel();  
        System.out.println("2 JFXPanel object created...");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(800, 560));
        add(jfxPanel, BorderLayout.CENTER);
        createScene();
    }

    private void createScene() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("3 createScene run metod started");
                stage = new Stage();
                System.out.println("4 createScene - stage created");
                stage.setTitle("Video");
                stage.setResizable(true);
                Group root = new Group();
                Scene scene = new Scene(root,80,20);
                stage.setScene(scene);
                System.out.println("5 createScene Group and Scene created - also set the Scene");
                //Set up the embedded browser:
                browser = new WebView();
                System.out.println("6 createScene - WbView created");
                webEngine = browser.getEngine();
                webEngine.load(videoUrl);
                System.out.println("7 createScene - Loeaded the video URL: " + videoUrl);
                ObservableList<Node> children = root.getChildren();
                children.add(browser);                     
                jfxPanel.setScene(scene);
                System.out.println("8 createScene - set the scene on the jfxPanel");
            }
        });
    }

    public void stopTrailer() { 
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println(":: stopTrailer() called");
                remove(jfxPanel);
                webEngine.load(null);
            }
        });
    }
}

最佳答案

我的 OSX 中也出现了同样的问题。当从 eclipse 运行时,一旦我为 jar 创建安装程序(.app),它就会运行文件并且它挂起。所以我找到了一个出路,在 Info.plist 文件中为 OSX 创建 .app 时,我增加了最小和最大内存。这解决了问题,现在运行顺利。不确定这是否能解决您的问题,但只是想与您分享。

谢谢!

关于java - Swing 组件内的 JFXPanel 将应用程序卡在 JFXPanel 构造上(os x 和 JDK 1.8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512518/

相关文章:

c++ - Windows .NET (C++) SpinWait 在 Linux 和 Mac OS X 上的等价物是什么?

java - 验证 double 值在两个值之间并且 GUI 打开两个 JFrame

java - Android CardView 动画不正确

swift - 单击按钮时无法加载 WebView

java - intellij Project make 和 Maven Compile 之间的区别?

swift - 捕获我的 macOS 应用程序菜单栏上的点击

java - findComponentAt 返回 JList 的 self

java - 在 JEditorPane 中查找和替换

Java Sql 'select sum' 给出不正确的值

java - java应用程序中所有级别url的主页按钮