【问题标题】:webview not running to use like web browserwebview 没有运行以像 web 浏览器一样使用
【发布时间】:2013-05-04 06:05:24
【问题描述】:

我正在尝试借助 javafx webview 创建一个 Web 浏览器。 我正在使用以下代码来创建 Web 浏览器。它在运行程序时给出错误异常

    package webviewbrowser;

    import com.sun.javaws.Main;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.ListChangeListener;
    import javafx.collections.ListChangeListener.Change;
    import javafx.concurrent.Worker.State;
    import javafx.event.ActionEvent;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.geometry.HPos;
    import javafx.geometry.Pos;
    import javafx.geometry.VPos;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Hyperlink;
    import javafx.scene.image.Image;

    import javafx.scene.image.ImageView;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.scene.layout.Region;
    import javafx.scene.paint.Color;
    import javafx.scene.web.PopupFeatures;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebHistory;
    import javafx.scene.web.WebHistory.Entry;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    import javafx.util.Callback;
    import netscape.javascript.JSObject;

     public class WebViewBrowser extends Application {

      private Scene scene;

      @Override
       public void start(Stage stage) {
        // create scene
        stage.setTitle("Web View");
        scene = new Scene(new Browser(), 750, 500, Color.web("#666970"));
        stage.setScene(scene);
        // apply CSS style
        scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
        // show stage
        stage.show();
       }

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

    class Browser extends Region {

     private HBox toolBar;
      private static String[] imageFiles = new String[]{
        "product.png",
        "blog.png",
        "forum.png",
        "partners.png",
        "help.png"
       };
      private static String[] captions = new String[]{
        "Products",
        "Blogs",
        "Forums",
        "Partners",
        "Help"
      };
     private static String[] urls = new String[]{
        "http://www.oracle.com/products/index.html",
        "http://blogs.oracle.com/",
        "http://forums.oracle.com/forums/",
        "http://www.oracle.com/partners/index.html",
        Main.class.getResource("help.html").toExternalForm()
      };
    final ImageView selectedImage = new ImageView();
    final Hyperlink[] hpls = new Hyperlink[captions.length];
    final Image[] images = new Image[imageFiles.length];
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    final Button hideAll = new Button("Hide All");
    final Button showAll = new Button("ShowAll");
    final WebView smallView = new WebView();
    final ComboBox comboBox = new ComboBox();
    private boolean needForumButtons = false;

         public Browser() {
         //apply the styles
         getStyleClass().add("browser");

        for (int i = 0; i < captions.length; i++) {
            // create hyperlinks
            Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
            Image image = images[i] =
                    new Image(getClass().getResourceAsStream(imageFiles[i]));
            hpl.setGraphic(new ImageView(image));
            final String url = urls[i];
            final boolean addButtons = (hpl.getText().equals("Forums"));

            // process event 
            hpl.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent e) {
                    needForumButtons = addButtons;
                    webEngine.load(url);
                }
            });
        }

        comboBox.setPrefWidth(60);

        // create the toolbar
        toolBar = new HBox();
        toolBar.setAlignment(Pos.CENTER);
        toolBar.getStyleClass().add("browser-toolbar");
        toolBar.getChildren().add(comboBox);
        toolBar.getChildren().addAll(hpls);
        toolBar.getChildren().add(createSpacer());

        //set actions for the buttons
        hideAll.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                webEngine.executeScript("hideAll()");
            }
        });

        showAll.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                webEngine.executeScript("showAll()");
            }
        });

        smallView.setPrefSize(120, 80);

        //handle popup windows
        webEngine.setCreatePopupHandler(
            new Callback<PopupFeatures, WebEngine>() {
                @Override public WebEngine call(PopupFeatures config) {
                    smallView.setFontScale(0.8);
                    if (!toolBar.getChildren().contains(smallView)) {
                        toolBar.getChildren().add(smallView);
                    }
                    return smallView.getEngine();
                }
             }
        );

        //process history
        final WebHistory history = webEngine.getHistory();
        history.getEntries().addListener(new 
            ListChangeListener<WebHistory.Entry>(){
                @Override
                public void onChanged(Change<? extends Entry> c) {
                    c.next();
                    for (Entry e : c.getRemoved()) {
                        comboBox.getItems().remove(e.getUrl());
                    }
                    for (Entry e : c.getAddedSubList()) {
                        comboBox.getItems().add(e.getUrl());
                    }
                }
        });

        //set the behavior for the history combobox               
        comboBox.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ev) {
                int offset =
                        comboBox.getSelectionModel().getSelectedIndex()
                        - history.getCurrentIndex();
                history.go(offset);
            }
        });



        // process page loading
        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                @Override
                public void changed(ObservableValue<? extends State> ov,
                    State oldState, State newState) {
                    toolBar.getChildren().removeAll(showAll, hideAll);    
                    if (newState == State.SUCCEEDED) {
                            JSObject win = 
                                (JSObject) webEngine.executeScript("window");
                            win.setMember("app", new JavaApp());
                            if (needForumButtons) {
                                toolBar.getChildren().addAll(showAll, hideAll);
                            }
                        }
                    }
                }
        );

        // load the home page        
        webEngine.load("http://www.oracle.com/products/index.html");

        //add components
        getChildren().add(toolBar);
        getChildren().add(browser);
    }

    // JavaScript interface object
    public class JavaApp {

        public void exit() {
            Platform.exit();
        }
    }

    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        double tbHeight = toolBar.prefHeight(w);
        layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
        layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
    }

    @Override
    protected double computePrefWidth(double height) {
        return 750;
    }

    @Override
    protected double computePrefHeight(double width) {
        return 600;
    }
}

【问题讨论】:

    标签: java webview javafx


    【解决方案1】:

    为我工作。

    代码只是引用了一些本地 html、css 和图像资源,这些资源未包含在您的问题中。因为如果您只是复制并粘贴并运行它们不存在,那么由于缺少资源文件,您会得到 NullPointerExceptions。


    对于任何想知道问题中的大部分代码来自哪里的人,它是 Oracle 的一部分 Adding HTML Content to JavaFX Applications。可以从教程链接以及可以加载运行程序的 NetBeans 项目下载与代码一起使用并允许其在不修改的情况下运行的其他资源(html、css 等)。


    我注释掉了对外部资源的引用,以便程序可以独立运行。并不是所有的图片等都很好地设计了样式。但它似乎对我来说很好用。

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.ListChangeListener;
    import javafx.concurrent.Worker.State;
    import javafx.event.ActionEvent;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.geometry.HPos;
    import javafx.geometry.Pos;
    import javafx.geometry.VPos;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Hyperlink;
    import javafx.scene.image.Image;
    
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.scene.layout.Region;
    import javafx.scene.paint.Color;
    import javafx.scene.web.PopupFeatures;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebHistory;
    import javafx.scene.web.WebHistory.Entry;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    import javafx.util.Callback;
    import netscape.javascript.JSObject;
    
    public class WebViewBrowser extends Application {
    
      private Scene scene;
    
      @Override
      public void start(Stage stage) {
        // create scene
        stage.setTitle("Web View");
        scene = new Scene(new Browser(), 750, 500, Color.web("#666970"));
        stage.setScene(scene);
        // apply CSS style
    //    scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
        // show stage
        stage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    
    class Browser extends Region {
    
      private HBox toolBar;
      private static String[] imageFiles = new String[]{
          "product.png",
          "blog.png",
          "forum.png",
          "partners.png",
          "help.png"
      };
      private static String[] captions = new String[]{
          "Products",
          "Blogs",
          "Forums",
          "Partners"
    //      "Help"
      };
      private static String[] urls = new String[]{
          "http://www.oracle.com/products/index.html",
          "http://blogs.oracle.com/",
          "http://forums.oracle.com/forums/",
          "http://www.oracle.com/partners/index.html"
    //      Main.class.getResource("help.html").toExternalForm()
      };
      final ImageView selectedImage = new ImageView();
      final Hyperlink[] hpls = new Hyperlink[captions.length];
      final Image[] images = new Image[imageFiles.length];
      final WebView browser = new WebView();
      final WebEngine webEngine = browser.getEngine();
      final Button hideAll = new Button("Hide All");
      final Button showAll = new Button("ShowAll");
      final WebView smallView = new WebView();
      final ComboBox comboBox = new ComboBox();
      private boolean needForumButtons = false;
    
      public Browser() {
        //apply the styles
        getStyleClass().add("browser");
    
        for (int i = 0; i < captions.length; i++) {
          // create hyperlinks
          Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
    //      Image image = images[i] =
    //          new Image(getClass().getResourceAsStream(imageFiles[i]));
    //      hpl.setGraphic(new ImageView(image));
          final String url = urls[i];
          final boolean addButtons = (hpl.getText().equals("Forums"));
    
          // process event
          hpl.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
              needForumButtons = addButtons;
              webEngine.load(url);
            }
          });
        }
    
        comboBox.setPrefWidth(60);
    
        // create the toolbar
        toolBar = new HBox();
        toolBar.setAlignment(Pos.CENTER);
        toolBar.getStyleClass().add("browser-toolbar");
        toolBar.getChildren().add(comboBox);
        toolBar.getChildren().addAll(hpls);
        toolBar.getChildren().add(createSpacer());
    
        //set actions for the buttons
        hideAll.setOnAction(new EventHandler() {
          @Override
          public void handle(Event t) {
            webEngine.executeScript("hideAll()");
          }
        });
    
        showAll.setOnAction(new EventHandler() {
          @Override
          public void handle(Event t) {
            webEngine.executeScript("showAll()");
          }
        });
    
        smallView.setPrefSize(120, 80);
    
        //handle popup windows
        webEngine.setCreatePopupHandler(
            new Callback<PopupFeatures, WebEngine>() {
              @Override public WebEngine call(PopupFeatures config) {
                smallView.setFontScale(0.8);
                if (!toolBar.getChildren().contains(smallView)) {
                  toolBar.getChildren().add(smallView);
                }
                return smallView.getEngine();
              }
            }
        );
    
        //process history
        final WebHistory history = webEngine.getHistory();
        history.getEntries().addListener(new
                                             ListChangeListener<WebHistory.Entry>(){
                                               @Override
                                               public void onChanged(Change<? extends Entry> c) {
                                                 c.next();
                                                 for (Entry e : c.getRemoved()) {
                                                   comboBox.getItems().remove(e.getUrl());
                                                 }
                                                 for (Entry e : c.getAddedSubList()) {
                                                   comboBox.getItems().add(e.getUrl());
                                                 }
                                               }
                                             });
    
        //set the behavior for the history combobox
        comboBox.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent ev) {
            int offset =
                comboBox.getSelectionModel().getSelectedIndex()
                    - history.getCurrentIndex();
            history.go(offset);
          }
        });
    
    
    
        // process page loading
        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
              @Override
              public void changed(ObservableValue<? extends State> ov,
                                  State oldState, State newState) {
                toolBar.getChildren().removeAll(showAll, hideAll);
                if (newState == State.SUCCEEDED) {
                  JSObject win =
                      (JSObject) webEngine.executeScript("window");
                  win.setMember("app", new JavaApp());
                  if (needForumButtons) {
                    toolBar.getChildren().addAll(showAll, hideAll);
                  }
                }
              }
            }
        );
    
        // load the home page
        webEngine.load("http://www.oracle.com/products/index.html");
    
        //add components
        getChildren().add(toolBar);
        getChildren().add(browser);
      }
    
      // JavaScript interface object
      public class JavaApp {
    
        public void exit() {
          Platform.exit();
        }
      }
    
      private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
      }
    
      @Override
      protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        double tbHeight = toolBar.prefHeight(w);
        layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
        layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
      }
    
      @Override
      protected double computePrefWidth(double height) {
        return 750;
      }
    
      @Override
      protected double computePrefHeight(double width) {
        return 600;
      }
    }
    

    很久以前我创建了一个WebView Web Browser。您可以看一下,以获得您的网络浏览器的一些灵感。我的浏览器中的代码需要进行一些清理和更改以利用 WebView 的一些更现代的内置功能,例如 WebView 的内置历史列表,但它是一个非常实用的选项卡式浏览器,也许你仍然会找到它有用。

    【讨论】:

    • 我可以从那里获得额外的资源
    • 来自我在答案中链接的WebView tutorial page 右侧的“其他文件”部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多