单击按钮时 WebEngine 未加载 URL

WebEngine not loading URL on button click

我正在用 JAVAFx 编写一个选项卡式 Web 浏览器。我面临的问题是:-
当我单击主页按钮 (HomeB) 时,它不会在当前选项卡中加载 DEFAULT_URL。这是我的代码中一些有用的部分。一些 body 请修复它。谢谢

        class Browser extends Region{  

            final BorderPane borderPane;  
            final TabPane    tabPane;
    private final HBox       toolbarMain;
                  WebView    browser    = new WebView();
            final  WebEngine webEngine  = browser.getEngine();
    private        String    DEFAULT_URL= "http://www.google.com";     
            final  TextField urlField   = new TextField(DEFAULT_URL);

    //Custom function for creation of New Tabs.  
    private Tab createAndSelectNewTab(final TabPane tabPane, final String title) {  
           Tab     tab         = new Tab(title);

            webEngine.locationProperty().addListener(new ChangeListener<String>() {  
                @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {  
                    urlField.setText(newValue); 
                }  
            }); 
           final ObservableList<Tab> tabs = tabPane.getTabs();  
           tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));  
           tabs.add(tabs.size() - 1, tab);  
           tabPane.getSelectionModel().select(tab);  
           return tab;  
    } 

    //Initialization function of the program.  
      public Browser() { 
                    borderPane        = new BorderPane();
                    tabPane           = new TabPane();
                    toolbarMain       = new HBox();
                    Button     HomeB  = new Button();
                    HomeB.setText("HOME");

           tabPane.setSide(Side.TOP);  

           final Tab newtab = new Tab();
           newtab.setText("+");


           newtab.setClosable(false); // this will not let the New Tab button(TAB) close  
           tabPane.getTabs().addAll(newtab); //Addition of New Tab to the tabpane.   
           createAndSelectNewTab(tabPane, "        "); 

           //Function to add and display new tabs with default URL display.  
           tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {  
                @Override  
                public void changed(ObservableValue<? extends Tab> observable,  
                          Tab oldSelectedTab, Tab newSelectedTab) {  
                     if (newSelectedTab == newtab) {  
                          Tab tab = new Tab();

                          //WebView - to display, browse web pages.  
                           WebView browser = new WebView();  
                          final WebEngine webEngine = browser.getEngine(); 
                          webEngine.load(DEFAULT_URL);  

                   EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {  
                               @Override public void handle(ActionEvent event) {  
                                    webEngine.load(urlField.getText().startsWith("http://")   
                                              ? urlField.getText()   
                                                        : "http://" + urlField.getText());  
                               }  
                          };  
                          urlField.setOnAction(goAction);

                          final VBox vBox = new VBox(5);  
                          vBox.getChildren().setAll(browser);  
                          VBox.setVgrow(browser, Priority.ALWAYS);  
                          tab.setContent(vBox); 
                          final ObservableList<Tab> tabs = tabPane.getTabs();  
                          tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));  
                          tabs.add(tabs.size() - 1, tab);  
                          tabPane.getSelectionModel().select(tab);  
                     }  
                }  
           }); 

//OnClick handling HomeB
        HomeB.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event){
                webEngine.load(DEFAULT_URL);
            }
        });

        toolbarMain.getChildren().addAll(HomeB,urlField);

//Placement of elements in borderpane
        borderPane.setCenter(tabPane);
        borderPane.setTop(toolbarMain);
        getChildren().add(borderPane);  
      }

}

当您单击 HomeB 时,默认 URL 加载到 browser,全局 WebView。这有效,但您没有看到 URL 已加载,因为您尚未将此浏览器添加到您的任何选项卡。

假设您为主页按钮创建了第一个选项卡:

tabPane.getTabs().addAll(newtab); // tab 0, then moves to 1

// Here you create a new tab, but put it on the 0 index:
createAndSelectNewTab(tabPane, "        "); 

// You can add now your global browser to the first tab:

final VBox vBoxIni = new VBox(5);  
vBoxIni.getChildren().setAll(browser);  
VBox.setVgrow(browser, Priority.ALWAYS);  
tabPane.getTabs().get(0).setContent(vBoxIni);  

其他选项将使用您为每个选项卡创建的本地 webView,并在活动选项卡上加载默认 URL:

HomeB.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        VBox vBox=(VBox)tabPane.getSelectionModel().getSelectedItem().getContent();
        if(vBox!=null){
            WebView webView=(WebView)vBox.getChildren().get(0);
            webView.getEngine().load(DEFAULT_URL);
        }
    }
});

请注意,这在第一个选项卡上不起作用,因为您尚未在此处设置任何内容。