2014-12-29 12 views

回答

0

是的,你可以很容易地使用webengine加載頁面 這裏網頁或網站添加到網頁視圖安靜是代碼,你可以使用:

final WebView browser = new WebView(); 
    final WebEngine webEngine = browser.getEngine(); 
      webEngine.load("http://www.favicon.cc/"); 

如果你想在這裏一個完整的應用程序是一個改編版上http://www.java2s.com/Code/Java/JavaFX/LoadwebpagefromURLtoWebView.htm的示例的:

package application; 

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

    public class Main extends Application { 

public void start(Stage stage) { 
    stage.setWidth(500); 
    stage.setHeight(500); 
    Scene scene = new Scene(new Group()); 
    VBox root = new VBox();  
    final WebView browser = new WebView(); 
    final WebEngine webEngine = browser.getEngine(); 
      webEngine.load("http://www.favicon.cc/"); 
    root.getChildren().add(browser); 
    scene.setRoot(root); 

    stage.setScene(scene); 
    stage.show(); 
} 

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

}

+0

嗯,我實際上是從網站意味着你在哪裏上。就像谷歌的favicon一樣。 – ZinXanCraft

1

下面的代碼片斷假定圖標應該是像大多數瀏覽器一樣放置在Tab中,爲簡單起見,它忽略了一些檢查。這是一種接受2個參數,url字符串和Tab的方法,並使用Google Favicon服務來獲取圖像。下載圖像的工作是由Image(在這種情況下,它在後臺的完成)完成:

void loadFavicon(String location, Tab tab) { 
    try { 
    String faviconUrl = String.format("http://www.google.com/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 
    } 

}