5

目前我有一个 TextField 这是我的地址栏和一个 WebView 这是我的页面。当我点击进入时,TextField 似乎没有做任何事情。它意味着运行 loadPage 方法并设置为页面以加载用户在地址栏中输入的任何内容。任何帮助,将不胜感激。

package javafx_webview;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

    WebEngine myWebEngine;

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

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Platinum v1");

        TextField addressBar = new TextField();

        addressBar.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                loadPage(event.toString());
            }
        });

        WebView myBrowser = new WebView();
        myWebEngine = myBrowser.getEngine();

        StackPane root = new StackPane();
        root.getChildren().add(myBrowser);
        root.getChildren().add(addressBar);
        primaryStage.setScene(new Scene(root, 640, 480));
        primaryStage.show();
    }

    private void loadPage(String url) {
        try {
            myWebEngine.load(url);
        } catch (Exception e) {
            System.out.println("The URL you requested could not be found.");
        }
    }
}
4

1 回答 1

6

从地址栏获取要加载的 url text,而不是地址栏toString上的操作事件。

final TextField addressBar = new TextField();
addressBar.setOnAction(new EventHandler<ActionEvent>() {
  public void handle(ActionEvent event) {
    myWebEngine.load(addressBar.getText());
  }
});

加载也是异步的,因此您的异常处理程序将不起作用。您需要监控 webengine loadworker 的异常属性以从引擎获取异常。另请注意,未找到 url 不一定是会报告的异常,相反,Web 服务器通常会返回一个 http 404 错误页面。

这是一个工作示例:

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;

public class Main extends Application {
  private WebEngine myWebEngine;

  public void start(Stage stage) {
    stage.setTitle("Platinum v1");

    final TextField addressBar = new TextField();

    addressBar.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent event) {
        myWebEngine.load(addressBar.getText());
      }
    });

    WebView myBrowser = new WebView();
    myWebEngine = myBrowser.getEngine();
    myWebEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
      @Override public void changed(ObservableValue<? extends Throwable> observableValue, Throwable oldException, Throwable exception) {
        System.out.println("WebView encountered an exception loading a page: " + exception);
      }
    });

    VBox root = new VBox();
    root.getChildren().setAll(
        addressBar,
        myBrowser
    );
    stage.setScene(new Scene(root));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}
于 2013-05-04T08:37:01.827 回答