JavaFX文本框事件处理程序

5

目前我有一个文本字段,它是我的地址栏,还有一个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.");
        }
    }
}
1个回答

6

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

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

此外,加载是异步的,因此您的异常处理程序不起作用。您需要监视webengine loadworker的exception属性以从引擎获取异常。还要注意,未找到的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); }
}

你说得对,异常没有被设置,那么你如何检测到404响应? - Martin Charlesworth
请提出一个新问题,涉及从WebView检测404 Web服务器响应。 - jewelsea

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接