捕获URL以在JavaFX WebView / WebEngine中使用 [英] Capturing a URL for use in JavaFX WebView/WebEngine
问题描述
我正在尝试创建媒体流应用程序。
I am trying to create a media streaming application.
我有一个包含两个JFXPanel的Jframe。左侧的JFXPanel包含一个WebView,用于加载来自www.mediacollege.com的示例视频剪辑链接目录。右侧的JFXPanel还包含播放视频内容的WebView。右边的面板目前正在播放www.mediacollege.com的嵌入式链接。
I have a Jframe that contains two JFXPanel's. The JFXPanel to the left contains a WebView that loads a directory of links of sample video clips from www.mediacollege.com. The JFXPanel to the right also contains a WebView that plays video content. The panel to the right is currently just playing an embedded link from www.mediacollege.com.
有人知道如何在点击它们时在左侧JFXPanel中捕获URL,以便我可以将它们移交给右侧面板中的WebView观看?
我们非常感谢您提供的任何帮助。
Would anybody know how would I capture the URL, s in the left JFXPanel when clicking on them so that I could then hand them over to the WebView in the right Panel for viewing? Any help on this would be greatly appreciated.
package exploration_02;
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Exploration_02 {
private static final String MEDIA_URL = "http://www.mediacollege.com/video-gallery/testclips/testcardm-snellwilcox.flv";
private static void initAndShowGUI() {
//Creating the Frame
JFrame frame = new JFrame("Exploration_02");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//Creating the Panels, buttons not in use.
final JButton jButton = new JButton("Button A");
final JButton jButton1 = new JButton("Button B");
final JFXPanel fxPanel = new JFXPanel();
final JFXPanel fxPanel1 = new JFXPanel();
//Adding the panels to the Frame
frame.add(jButton);
frame.add(jButton1);
frame.add(fxPanel);
frame.add(fxPanel1);
frame.setVisible(true);
//Panel and Button Params
jButton.setSize(new Dimension(200, 27));
fxPanel.setSize(new Dimension(400, 450));
fxPanel.setLocation(new Point(0, 27));
jButton1.setSize(new Dimension(200, 27));
jButton1.setLocation(new Point(501, 0));
fxPanel1.setSize(new Dimension(550, 450));
fxPanel1.setLocation(new Point(501, 27));
frame.getContentPane().setPreferredSize(new Dimension(1100, 580));
frame.pack();
frame.setResizable(false);
Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
@Override
public void run() {
initFX(fxPanel);
}
});
Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
@Override
public void run() {
initFX1(fxPanel1);
}
});
}
/* Creates a WebView and navigates to links site */
private static void initFX(final JFXPanel fxPanel) {
Group group = new Group();
Scene scene = new Scene(group, Color.BLUE);
fxPanel.setScene(scene);
WebView webView = new WebView();
group.getChildren().add(webView);
webView.setMinSize(300, 300);
webView.setMaxSize(400, 300);
// Obtain the webEngine.
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.mediacollege.com/video-gallery/testclips/");
}
//Creates a WebView for viewing the media files.
private static void initFX1(final JFXPanel fxPanel1) {
Group group1 = new Group();
Scene scene1 = new Scene(group1, Color.RED);
fxPanel1.setScene(scene1);
WebView webView1 = new WebView();
group1.getChildren().add(webView1);
webView1.setMinSize(300, 300);
webView1.setMaxSize(400, 300);
WebEngine webEngine1 = webView1.getEngine();
webEngine1.loadContent(
"<video width='360' height='288'controls='controls'>"
+ "<source src='http://mediacollege.com/video-gallery/testclips/20051210-w50s.flv'/>"
+ "Your browser does not support the video tag."
+ "</video>");
}
/* Start application */
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
}
推荐答案
你我需要将ChangeListener添加到WebView的WebEngine中。 JavaFX WebView addHyperlinkListener 解释如何这样做。
You'll need to add a ChangeListener to the WebEngine of the WebView. "JavaFX WebView addHyperlinkListener" explains how to do this.
这篇关于捕获URL以在JavaFX WebView / WebEngine中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!