2

我在JavaFX2.0中编写了以下代码

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

/**
 *
 * @author roger
 */
public class WebViewDemo extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        WebView x = new WebView();
        WebEngine ex = x.getEngine();
        ex.load("http://www.google.com");
        root.getChildren().add(x);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我收到以下错误,无法弄清楚出了什么问题?

Jul 03, 2012 11:18:42 PM com.sun.webpane.webkit.network.URLLoader doRun
WARNING: Unexpected error
java.lang.IllegalArgumentException: Illegal cookie name
    at java.net.HttpCookie.<init>(HttpCookie.java:158)
    at java.net.HttpCookie.parseInternal(HttpCookie.java:964)
    at java.net.HttpCookie.parse(HttpCookie.java:215)
    at java.net.HttpCookie.access$100(HttpCookie.java:58)
    at java.net.HttpCookie$12.parse(HttpCookie.java:1096)
    at sun.net.www.protocol.http.HttpURLConnection.filterHeaderField(HttpURLConnection.java:2605)
    at sun.net.www.protocol.http.HttpURLConnection.getFilteredHeaderFields(HttpURLConnection.java:2642)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2686)
    at com.sun.webpane.webkit.network.URLLoader.extractHeaders(URLLoader.java:776)
    at com.sun.webpane.webkit.network.URLLoader.willSendRequest(URLLoader.java:574)
    at com.sun.webpane.webkit.network.URLLoader.doRun(URLLoader.java:155)
    at com.sun.webpane.webkit.network.URLLoader.access$000(URLLoader.java:42)
    at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:104)
    at com.sun.webpane.webkit.network.URLLoader$1.run(URLLoader.java:101)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.webpane.webkit.network.URLLoader.run(URLLoader.java:101)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

请让我知道需要修改什么以及出了什么问题?

4

1 回答 1

2

一些 google-fu 我发现这个论坛帖子导致了这个错误RT-15676。该帖子建议添加以下行:

java.net.CookieHandler.setDefault(null);

在应用程序启动之后但在启动 WebView 之前。所以我在将根 StackPane 添加到场景之前放置了它。它现在似乎起作用了。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewDemo extends Application {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        WebView x = new WebView();
        WebEngine ex = x.getEngine();
        ex.load("http://www.google.com");

        root.getChildren().add(x);
        java.net.CookieHandler.setDefault(null);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
于 2012-07-03T19:14:33.223 回答