如何在不每次加载 URL 的情况下从 JPanel 调整 children WebView/JFXPanel 的大小?

How to resize the children WebView/JFXPanel from the JPanel without loading URL every time?

我在 TabbedPane 中有一个 JPanel,其中包含一个 JFXPanel 和一个 WebView,我在面板上设置了一个 ComponentListener,在每次我调整面板大小时,为了调整其 child、JFXPanelWebView 的大小。我想要的是每次调整面板大小时都不会加载 URL(网站)的内容。我希望内容保持加载状态并且只调整大小。

正在加载的内容是网站的主页,例如,当我调整面板大小时,我被重定向到该主页,我设置为 URL 但我不想丢失我当前打开的页面。

避免重新加载的第二个原因是有时它会持续几秒钟,我想避免这种过载。

我最初尝试使用静态变量,以便只打开一次 URL,但是在调整大小时,页面不再显示...只显示一个白色页面。

代码如下:

public class RtcOverview extends JPanel {

String url = "http://10.112.85.142:8080/petshopJSF/";

public RtcOverview() {
    super();
    this.setVisible(true);
    this.doLayout();
    this.add(jfxPanel);
    this.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
            initComponents();
        }
    });
}

private void initComponents() {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            final WebView view = new WebView();
            int width = getParent().getWidth();
            int height = getParent().getHeight();

            view.setMinSize(width, height);
            view.setPrefSize(width, height);

            engine = view.getEngine();
            engine.load(url);

            Scene scene = new Scene(view);
            jfxPanel.setScene(scene);

            Platform.setImplicitExit(false);
        }
    });
}
}

Andrew Thompson 在上面的评论中给了我这个答案。无需在每次调整 JPanel 大小时都调用方法 initComponents()。而不是使用这个块

this.addComponentListener(new java.awt.event.ComponentAdapter() {
    public void componentResized(ComponentEvent e) {
        initComponents();
    }
});

我用了下面的

this.setLayout(new BorderLayout());

this.add(jfxPanel, BorderLayout.CENTER);

它奏效了。 JFXPanel 像其父 JPanel 一样调整大小,无需再次加载 url 并每次都初始化所有变量。