JavaFX WebView 增长以填充整个区域 [英] JavaFX WebView grow to fill entire area
问题描述
我正在尝试制作一个扩展到周围 JPanel 的 javafx WebView.根据这个线程:
注意场景有一个蓝色背景,所以你可以看到 javafx 的东西确实填满了整个 JPanel.但是WebView并没有填充javafx Group/Scene.
import java.awt.BorderLayout;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JPanel;导入 javax.swing.SwingUtilities;导入 javafx.application.Platform;导入 javafx.embed.swing.JFXPanel;导入 javafx.scene.Group;导入 javafx.scene.Scene;导入 javafx.scene.layout.GridPane;导入 javafx.scene.layout.Priority;导入 javafx.scene.paint.Color;导入 javafx.scene.web.WebEngine;导入 javafx.scene.web.WebView;公共课主要{私人无效initAndShowGUI(){//该方法在 EDT 线程上调用JFrame frame = new JFrame("Swing and JavaFX");frame.setSize(1000, 1000);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel 面板 = new JPanel(new BorderLayout(0, 0));frame.getContentPane().add(panel, BorderLayout.CENTER);JButton button = new JButton("button");panel.add(button, BorderLayout.CENTER);最终 JFXPanel fxPanel = 新 JFXPanel();panel.add(fxPanel, BorderLayout.CENTER);Platform.runLater(new Runnable() {@覆盖公共无效运行(){initFX(fxPanel);}});}私有 WebView webView;私人无效initFX(JFXPanel fxPanel){//该方法在 JavaFX 线程上调用场景场景 = createScene();fxPanel.setScene(场景);GridPane.setHgrow(webView, Priority.ALWAYS);GridPane.setVgrow(webView, Priority.ALWAYS);}私人场景 createScene() {组根 = new Group();场景场景 = 新场景(根,Color.ALICEBLUE);webView = new WebView();GridPane.setHgrow(webView, Priority.ALWAYS);GridPane.setVgrow(webView, Priority.ALWAYS);WebEngine webEngine = webView.getEngine();webEngine.load("http://www.google.com");root.getChildren().add(webView);GridPane.setHgrow(webView, Priority.ALWAYS);GridPane.setVgrow(webView, Priority.ALWAYS);返回场景;}公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(new Runnable() {@覆盖公共无效运行(){new Main().initAndShowGUI();}});}}
一种方法是将 WebView
添加到
import java.awt.BorderLayout;导入 javax.swing.JFrame;导入 javax.swing.SwingUtilities;导入 javafx.application.Platform;导入 javafx.embed.swing.JFXPanel;导入 javafx.scene.Scene;导入 javafx.scene.layout.StackPane;导入 javafx.scene.web.WebEngine;导入 javafx.scene.web.WebView;/** @see https://stackoverflow.com/a/31576647/230513 */公共类 WebViewTest {私人无效initAndShowGUI(){//该方法在 EDT 线程上调用JFrame frame = new JFrame("Swing and JavaFX");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);最终 JFXPanel fxPanel = 新 JFXPanel(){@覆盖公共维度 getPreferredSize() {返回新维度(640, 480);}};frame.add(fxPanel, BorderLayout.CENTER);框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);Platform.runLater(() -> {initFX(fxPanel);});}私人无效initFX(JFXPanel fxPanel){//该方法在 JavaFX 线程上调用场景场景 = createScene();fxPanel.setScene(场景);}私人场景 createScene() {StackPane root = new StackPane();场景场景 = 新场景(根);WebView webView = new WebView();WebEngine webEngine = webView.getEngine();webEngine.load("http://www.example.com");root.getChildren().add(webView);返回场景;}公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(new WebViewTest()::initAndShowGUI);}}
I'm trying to make a javafx WebView that expands to the surrounding JPanel. According to this thread:
Note that the Scene has a blue background, so you can see that the javafx stuff does fill the entire JPanel. But the WebView does not fill the javafx Group/Scene.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class Main {
private void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX");
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout(0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);
JButton button = new JButton("button");
panel.add(button, BorderLayout.CENTER);
final JFXPanel fxPanel = new JFXPanel();
panel.add(fxPanel, BorderLayout.CENTER);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
}
private WebView webView;
private void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
GridPane.setHgrow(webView, Priority.ALWAYS);
GridPane.setVgrow(webView, Priority.ALWAYS);
}
private Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
webView = new WebView();
GridPane.setHgrow(webView, Priority.ALWAYS);
GridPane.setVgrow(webView, Priority.ALWAYS);
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.google.com");
root.getChildren().add(webView);
GridPane.setHgrow(webView, Priority.ALWAYS);
GridPane.setVgrow(webView, Priority.ALWAYS);
return scene;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main().initAndShowGUI();
}
});
}
}
One approach is to add the WebView
to a StackPane
, which "will attempt to resize each child to fill its content area." I've given the enclosing JFXPanel
to an arbitrary preferred size of 640 x 480
; resize the frame to see how the StackPane
reflows the WebView
content based on the default Pos.CENTER
.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
/** @see https://stackoverflow.com/a/31576647/230513 */
public class WebViewTest {
private void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFXPanel fxPanel = new JFXPanel(){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
frame.add(fxPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Platform.runLater(() -> {
initFX(fxPanel);
});
}
private void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private Scene createScene() {
StackPane root = new StackPane();
Scene scene = new Scene(root);
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.example.com");
root.getChildren().add(webView);
return scene;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new WebViewTest()::initAndShowGUI);
}
}
这篇关于JavaFX WebView 增长以填充整个区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!