JavaFX WebView成长为填充整个区域

我试图做一个扩展到周围的JPanel的javafx WebView。 根据这个线程: http : //www.coderanch.com/t/634791/JavaFX/java/Resizing-HTMLEditor-JavaFX ,在WebView中有一个错误,你需要调用GridPane.setHgrowGridPane.setVgrow的WebView。 我用这些调用的代码,但WebView仍然是600x800px。 jxframe悲伤

请注意,场景有一个蓝色的背景,所以你可以看到javafx的东西填充整个JPanel。 但WebView不会填充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(); } }); } } 

一种方法是将WebView添加到StackPane ,该“试图调整每个孩子的大小以填充其内容区域”。 我已经给JFXPanel封装了640 x 480的任意首选大小; 调整框架的大小以查看StackPane如何基于默认的Pos.CENTER WebView内容。

图片

 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); } } 

其实,我想我得到了一个基于http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm的解决scheme:;

 import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.geometry.HPos; import javafx.geometry.VPos; import javafx.scene.Scene; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class WebViewSample { public static void main(String[] args) { 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); final JFXPanel jfxPanel = new JFXPanel(); panel.add(jfxPanel); Platform.runLater(new Runnable() { @Override public void run() { initFx(jfxPanel); } }); } private static void initFx(JFXPanel fxPanel) { Stage stage = new Stage(); stage.setTitle("Web View"); Scene scene = new Scene(new Browser(), 1000, 1000, Color.web("#666970")); stage.setScene(scene); fxPanel.setScene(scene); } } class Browser extends Region { final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); public Browser() { // load the home page webEngine.load("http://www.google.com"); //add components getChildren().add(browser); } @Override protected void layoutChildren() { double w = getWidth(); double h = getHeight(); layoutInArea(browser,0,0,w,h,0,HPos.CENTER,VPos.CENTER); } } 

我想诀窍是子类的区域,以便您可以手动布局WebView的位置。