gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:05:35 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com