无法更新网址和更新Webview

编程入门 行业动态 更新时间:2024-10-26 02:34:23
本文介绍了无法更新网址和更新Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的项目设置,我正在尝试更新Webview,因此每个选项卡将由一个地址栏更新.我似乎找不到我要去哪里.我是javaFX的新手.我知道我必须在其中为搜索按钮添加另一个更改侦听器,只是不确定如何...我找不到如何执行此操作的任何内容.除了一个示例并没有真正解释任何内容并且代码凌乱.

This is my project setup I'm trying to update the webview so each tab will be updated by one address bar. I cant seem to find where I'm going wrong. I'm new to javaFX. I know I have to add another change listener in there for the search button just not sure how...I cant find anything on how to do this. except one example that didnt really explain anything and the code was messy.

public class Client extends Application { private Scene scene; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); BorderPane root = new BorderPane(); Browser browseR = new Browser(); final TabPane tabs = new TabPane(); final Tab home = new Tab("Home"); final Tab newtab = new Tab("+"); HBox hbox = new HBox(); public static TextField addressBar = new TextField(); final Group group = new Group(tabs,root,hbox); //joins the borderpane and browser element together @Override public void start(Stage stage) { // create the scene stage.setTitle("Web Browser"); scene = new Scene(group,screenSize.getWidth(),screenSize.getHeight(), Color.web("#666970")); //create scene with root pane and browser pane hbox.setSpacing(10); hbox.setStyle("-fx-background-color: #FFFFFF;"); hbox.setPadding(new Insets(5, 24, 5, 12)); Button backButton = new Button("<"); backButton.setOnAction(this::backButtonHandler); backButton.setPrefSize(25, 20); Button forwardButton = new Button(">"); forwardButton.setOnAction(this::forwardButtonHandler); forwardButton.setPrefSize(25, 20); HBox.setHgrow(addressBar, Priority.ALWAYS); //addressBar.setMaxWidth(screenSize.getWidth()-200); Button searchButton = new Button("go"); searchButton.setOnAction(this::searchButtonHandler); //add all menu elements to the top of the screen hbox.getChildren().addAll(backButton, forwardButton,addressBar,searchButton); setConstraints(tabs,hbox,stage); createHomeTab(); createNewTab(); tabs.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() { @Override public void changed(ObservableValue<? extends Tab> observable, Tab oldSelectedTab, Tab newSelectedTab) { if(newSelectedTab == newtab) { final Tab tab = new Tab(browseR.getWebTitle()); tabs.getTabs().add(tab); tabs.getSelectionModel().select(tab); tab.setContent(new Browser()); loadFavicon(addressBar.getText(),tab); } } }); root.setTop(hbox); root.setCenter(tabs); browseR.updateAddress(); stage.setScene(scene); stage.show(); } public static void setConstraints(TabPane tabs, HBox hbox, Stage stage) { hbox.prefWidthProperty().bind(stage.widthProperty()); tabs.prefHeightProperty().bind(stage.heightProperty()); } private void backButtonHandler(ActionEvent event) { browseR.goBack(); } public void createHomeTab() { tabs.getTabs().addAll(newtab,home); tabs.getSelectionModel().select(home); home.setContent(new Browser()); loadFavicon(Browser.getHomePage(),home); } private void forwardButtonHandler(ActionEvent event) { browseR.goForward(); } public void createNewTab() { newtab.setText(" + "); newtab.setClosable(false); } private void searchButtonHandler(ActionEvent event) { Browser.navigate(addressBar.getText()); } void loadFavicon(String location, Tab tab) { try { String faviconUrl = String.format("www.google/s2/favicons?domain_url=%s", URLEncoder.encode(location, "UTF-8")); Image favicon = new Image(faviconUrl, true); ImageView iv = new ImageView(favicon); tab.setGraphic(iv); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); // not expected } } public static void main(String[] args){ //new Config(); launch(args); } } public class Browser extends Region { public static WebView browser = new WebView(); public static WebEngine webEngine = getBrowser().getEngine(); static String[] websites = {"www.google"}; private static String homePage = websites[0]; public Browser() { //apply the styles getStyleClass().add("browser"); // load the web page webEngine.load(homePage); //add the web view to the scene getChildren().add(getBrowser()); } public static void navigate(String website) { String google = "www.google/search?q="; String charset = "UTF-8"; if(website.contains("http")) { webEngine.load(website); //debug System.out.println("found http"); } else { try { webEngine.load(google+ URLEncoder.encode(website, charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //debug System.out.println(website); } /* private Node createSpacer() { Region spacer = new Region(); HBox.setHgrow(spacer, Priority.ALWAYS); return spacer; } */ public String getWebTitle() { return webEngine.getTitle(); } public void goBack() { Platform.runLater(() -> { webEngine.executeScript("history.back()"); }); } public String updateAddress() { webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> { if (Worker.State.SUCCEEDED.equals(newValue)) { Client.addressBar.setText(webEngine.getLocation()); } }); return null; } public void goForward() { Platform.runLater(() -> { webEngine.executeScript("history.forward()"); }); } @Override protected void layoutChildren() { double w = getWidth(); double h = getHeight(); layoutInArea(getBrowser(),0,0,w,h,0, HPos.CENTER, VPos.CENTER); } @Override protected double computePrefWidth(double height) { return 750; } @Override protected double computePrefHeight(double width) { return 500; } //getters and setters public static String getHomePage() { return homePage; } public static void setHomePage(String homePage) { Browser.homePage = homePage; } public static WebView getBrowser() { return browser; }

推荐答案

如果我正确理解了这个问题,则您将拥有一个文本字段和多个选项卡,每个选项卡都带有一个浏览器.当文本字段上发生操作时,您想更新浏览器在当前选定选项卡中的位置.

If I understand the question correctly, you have a single text field, and multiple tabs, each with a browser. When an action occurs on the text field, you want to update the location of the browser in the currently selected tab.

最简单的方法是保留对当前显示的浏览器的引用:

The easiest way is to keep a reference to the currently displayed browser:

public class Client extends Application { private Browser displayedBrowser ; // ... }

创建新的标签页和浏览器时,请在标签页的selectedProperty中添加一个侦听器,以更新displayedBrowser字段:

When you create a new tab and browser, add a listener to the tab's selectedProperty which updates the displayedBrowser field:

tabs.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() { @Override public void changed(ObservableValue<? extends Tab> observable, Tab oldSelectedTab, Tab newSelectedTab) { if(newSelectedTab == newtab) { final Tab tab = new Tab(browseR.getWebTitle()); tabs.getTabs().add(tab); Browser newBrowser = new Browser(); tab.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { if (isNowSelected) { displayedBrowser = newBrowser ; } }); tabs.getSelectionModel().select(tab); tab.setContent(newBrowser); loadFavicon(addressBar.getText(),tab); } } });

现在,displayedBrowser将始终指向当前选定选项卡中的浏览器.

Now displayedBrowser will always point to the browser in the currently-selected tab.

很明显,navigate不能是静态方法,因为您要浏览特定的浏览器:实际上,Browser类中的 nothing 应该是静态的:remove all Browser类中的static关键字的出现.进行这些更改后,您只需更新当前的浏览器即可:

Obviously, navigate cannot be a static method, since you want to navigate a specific browser: in fact nothing in the Browser class should be static: remove all occurrences of the static keyword from the Browser class. Once you make those changes you can just update the current browser:

private void searchButtonHandler(ActionEvent event) { displayedBrowser.navigate(addressBar.getText()); }

更多推荐

无法更新网址和更新Webview

本文发布于:2023-10-31 18:34:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   Webview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!