我正在swing组件(JPanel)中运行一个JPanel。我遇到的问题是,有时,并不是每次创建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 (小牛)JDK1.8上测试过这一点。在我看来,这有点像一个JavaFX /OSXJDK1.8bug--但我还没有在网上找到任何关于它的信息。
有人有线索吗?有没有办法调试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);
}
});
}
}发布于 2014-12-15 09:11:30
同样的问题出现在我的OSX上。当从eclipse运行时,当我为jar创建一个安装程序(.app)并挂起它时,它正在运行文件。因此,我找到了一条出路,在Info.plist文件中为OSX创建Info.plist时,我增加了最小内存和最大内存。解决了问题,现在进展顺利。不确定这是否能解决你的问题,只是想和你分享一下。
谢谢!
发布于 2015-03-10 02:39:34
在stopTrailer方法中,在JavaFX线程中运行时,将从其swing容器中移除JavaFx面板。
Platform.runLater(new Runnable() {
...
remove(jfxPanel);
webEngine.load(null);
}
});在类似的代码中,我还错误地在JavaFX线程上添加了JavaFX,在mac上也有相同的挂起。当我发现并删除坏的添加时,代码停止挂起。
我相信,在Windows和Linux上,由于AWT和JavaFX线程是完全不同的,所以代码虽然不好,但不会造成灾难。但是,在mac上,底层UI事件必须发生在同一个原生OS线程上,从jfxPanel线程中删除或添加JavaFx几乎肯定会导致AWT/Swing和JavaFX最终导致这些UI资源陷入死锁。
https://stackoverflow.com/questions/26512518
复制相似问题