我如何通过单击按钮清除带有 javaFX 的 WebView 的内容?

How i can clear the WebView's content with javaFX by clicking on a button?

我想知道如何通过使用javaFX点击一个按钮来清除webview的内容?那我该怎么做呢?例如:WebView1 包含 google 网页,同时单击按钮应该清除此页面

您可以通过加载一些空内容来清除网络视图:

webView.getEngine().loadContent("");

完整示例:

import java.util.regex.Pattern;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebBrowser extends Application {

    private TextField textField ;
    private WebView webView ;

    @Override
    public void start(Stage primaryStage) {
        webView = new WebView();
        textField = new TextField("https://www.google.com");
        textField.setOnAction(e -> load());
        load();

        Button clearButton = new Button("Clear");
        clearButton.setOnAction(e -> webView.getEngine().loadContent(""));
        BorderPane root = new BorderPane(webView, new HBox(10, textField, clearButton), null, null, null);

        textField.setPadding(new Insets(5));
        root.setPadding(new Insets(5));
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    private void load() {
        String url = makeUrl(textField.getText()) ;
        webView.getEngine().load(url);
        textField.setText(url);
    }


    private String makeUrl(String text) {
        if (Pattern.matches("[a-zA-Z]+:.+", text)) {
            return text ;
        } else return "http://"+text ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}