我在一个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时,我增加了最小和最大内存。该问题已解决,现在运行顺利。不知道这是否可以解决您的问题,但只是想与您分享。
谢谢 !