0
public class WebWiewYahoo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        VBox root = new VBox();
        Button go = new Button("go");
        final TextField address = new TextField("http://www.yahoo.com");
        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();
        HBox toolBar1 = new HBox();

        toolBar1.setAlignment(Pos.TOP_LEFT);
        toolBar1.getChildren().addAll(address, go);

        // load the web page
        webEngine.load("http://www.yahoo.com");

        root.getChildren().add(toolBar1);
        root.getChildren().add(browser);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}

//我想在浏览器中操作链接。因此,即使在任何时候单击任何链接,我都可以将其重定向到 yahoo.com。请帮助。

4

1 回答 1

0

你可以尝试这样做:

webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
    @Override
    public void changed(ObservableValue<? extends State> paramObservableValue,
                        State oldState,
                        State newState) {
        if (State.SCHEDULED == newState && !"http://www.yahoo.com/".equals(webEngine.getLocation())) {
            webEngine.load("http://www.yahoo.com");
        }
    }
});
于 2013-03-29T11:27:54.883 回答