本文介绍了JavaFX WebView扩展到整个区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个扩展到周围JPanel的javafx WebView。根据这个帖子:



请注意,场景有蓝色背景,所以你可以看到javafx的东西确实填满了整个JPanel。但是WebView没有填充javafx组/场景。

  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;


公共类Main {

private void initAndShowGUI(){
//此方法在EDT线程
JFrame框架上调用= 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);
}
});
}

私有WebView webView;

private void initFX(JFXPanel fxPanel){
//此方法在JavaFX线程上调用
Scene scene = createScene();
fxPanel.setScene(scene);

GridPane.setHgrow(webView,Priority.ALWAYS);
GridPane.setVgrow(webView,Priority.ALWAYS);
}

private Scene createScene(){
Group root = new Group();
场景场景=新场景(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);

返回场景;
}

public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run (){
new Main()。initAndShowGUI();
}
});
}
}


解决方案

一个方法是将 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 * /
公共类WebViewTest {

private void initAndShowGUI(){
//在EDT线程上调用此方法
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){
//此方法在JavaFX线程上调用
场景场景= createScene();
fxPanel.setScene(scene);
}

private场景createScene(){
StackPane root = new StackPane();
场景场景=新场景(根);
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.load(http://www.example.com);
root.getChildren()。add(webView);
返回场景;
}

public static void main(String [] 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扩展到整个区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:12