我正在研究特定的网页,并且我想将javafx.embed.swing.JFXPanel及其功能和swing GUI联系在一起。我想找到jfxPanelJTabbedPane吗?这是JFXPanel的两种方法。

private JFXPanel getSearchPanel() {
    jfxPanel = new JFXPanel();
    createScene();
    setLayout(new BorderLayout());
    jfxPanel.setVisible(true);
    return jfxPanel;
}

private void createScene() {
    PlatformImpl.startup(new Runnable() {
        @Override
        public void run() {
            Pane root = new WebViewPane();
            Scene scene = new Scene(root, 80, 50);
            jfxPanel.setScene(scene);
        }
    });
}

和为浏览器创建JTabbedPane的方法:
private void createNewTab() {
        JPanel panel = new JPanel(new BorderLayout());
        startPage = startPageField.getText();
        panel.add(new JScrollPane(getSearchPanel()), BorderLayout.CENTER);
        tabPane.setBackground(Color.PINK);
        tabPane.addTab(startPage, panel);
    }

感谢您的帮助或贡献!

最佳答案

使用example从此 javafx.scene.web.WebView 开始,我进行了以下更改以产生如下所示的结果。 createTab()方法可确保FXPanel具有合理的首选大小,并确保在JavaFX线程上创建内容。

JTabbedPane jtp = new JTabbedPane();
jtp.add("One", createTab("http://www.example.com"));
jtp.add("Two", createTab("http://www.example.net"));
jtp.add("Three", createTab("http://www.example.org"));
frame.add(jtp, BorderLayout.CENTER);

java - 如何在JTabbedPane的JFXPanel中找到WebView?-LMLPHP
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
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/44091479/230513
 * @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("WebViewTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTabbedPane jtp = new JTabbedPane();
        jtp.add("One", createTab("http://www.example.com"));
        jtp.add("Two", createTab("http://www.example.net"));
        jtp.add("Three", createTab("http://www.example.org"));
        frame.add(jtp, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JFXPanel createTab(String s) {
        final JFXPanel fxPanel = new JFXPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        Platform.runLater(() -> {
            initFX(fxPanel, s);
        });
        return fxPanel;
    }

    private void initFX(JFXPanel fxPanel, String s) {
        // This method is invoked on the JavaFX thread
        StackPane root = new StackPane();
        Scene scene = new Scene(root);
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load(s);
        root.getChildren().add(webView);
        fxPanel.setScene(scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new WebViewTest()::initAndShowGUI);
    }
}

通常,其他Web引擎实现为not supported

10-08 02:20