《JavaFX应用程序添加HTML内容》-添加WebView组件到应用程序场景
来源:互联网 发布:詹姆斯2016总决赛数据 编辑:程序博客网 时间:2024/06/10 20:43
原文链接
添加WebView组件到应用程序场景
本章介绍了WebViewSample应用程序和解释了如何添加WebView组件到JavaFX应用程序场景。
WebViewSample应用程序创建了Browser类封装了WebView对象和UI控件工具栏。WebViewSample类创建场景和添加Browser对象到场景。
创建一个嵌入式浏览器
例4-1展示了如何添加WebView组件到应用程序场景。
例4-1 使用WebView和WebEngine类创建一个浏览器
import javafx.application.Application;import javafx.geometry.HPos;import javafx.geometry.VPos;import javafx.scene.Node;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.layout.Priority;import javafx.scene.layout.Region;import javafx.scene.paint.Color;import javafx.scene.web.WebEngine;import javafx.scene.web.WebView;import javafx.stage.Stage;public class WebViewSample extends Application { private Scene scene; @Override public void start(Stage stage) { // create the scene stage.setTitle("Web View"); scene = new Scene(new Browser(),900,600, Color.web("#666970")); stage.setScene(scene); scene.getStylesheets().add("webviewsample/BrowserToolbar.css"); stage.show(); } public static void main(String[] args){ launch(args); }}class Browser extends Region { final WebView browser = new WebView();final WebEngine webEngine = browser.getEngine(); public Browser() { //apply the styles getStyleClass().add("browser"); // load the web page webEngine.load("http://www.oracle.com/products/index.html"); //add the web view to the scene getChildren().add(browser); } private Node createSpacer() { Region spacer = new Region(); HBox.setHgrow(spacer, Priority.ALWAYS); return spacer; } @Override protected void layoutChildren() { double w = getWidth(); double h = getHeight(); layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER); } @Override protected double computePrefWidth(double height) { return 900; } @Override protected double computePrefHeight(double width) { return 600; }}
在这段代码中,web引擎加载指向甲骨文公司网站的URL。通过使用getChildren和add方法把包含web引擎的WebView对象添加到应用程序场景。
当您添加、编译和运行这个代码片段,它生成应用程序窗口如图4-1所示。
图4-1 在应用程序场景中的WebView对象
“图4-1 在应用程序场景中的WebView对象”的描述
创建一个应用程序工具栏
添加一个带有四个Hyperlink对象的工具栏为了在不同的Oracle web资源间进行切换。在例4-2中学习修改后的Browser类的代码。它添加了可替代的web资源URL,包括甲骨文产品、博客、Java文档、和合作伙伴网络。代码片段也创建了一个工具栏并添加超链接。
例4-2 创建工具栏
import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.geometry.HPos;import javafx.geometry.VPos;import javafx.scene.Node;import javafx.scene.Scene;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.WebEngine;import javafx.scene.web.WebView;import javafx.stage.Stage;public class WebViewSample extends Application { private Scene scene; @Override public void start(Stage stage) { // create scene stage.setTitle("Web View"); scene = new Scene(new Browser(),900,600, Color.web("#666970")); stage.setScene(scene); 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; final private static String[] imageFiles = new String[]{ "product.png", "blog.png", "documentation.png", "partners.png" }; final private static String[] captions = new String[]{ "Products", "Blogs", "Documentation", "Partners" }; final private static String[] urls = new String[]{ "http://www.oracle.com/products/index.html", "http://blogs.oracle.com/", "http://docs.oracle.com/javase/index.html", "http://www.oracle.com/partners/index.html" }; 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(); public Browser() { //apply the styles getStyleClass().add("browser"); for (int i = 0; i < captions.length; i++) {final 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];hpl.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent e) {webEngine.load(url);}});} // load the home page webEngine.load("http://www.oracle.com/products/index.html");// create the toolbar toolBar = new HBox();toolBar.getStyleClass().add("browser-toolbar");toolBar.getChildren().addAll(hpls); //add components getChildren().add(toolBar); getChildren().add(browser); } 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 900; } @Override protected double computePrefHeight(double width) { return 600; }}
这段代码使用一个for循环创建超链接。setOnAction方法定义超链接行为。当用户点击一个链接,相应的URL值传递给webEngine的load方法。当您编译并运行修改后的应用程序时,应用程序窗口变化如图4-2所示。
图4-2 带有导航工具栏的嵌入式浏览器
“图4-2 带有导航工具栏的嵌入式浏览器”的描述
上一篇:支持HTML5特性
下一篇:处理JavaScript命令
- 《JavaFX应用程序添加HTML内容》-添加WebView组件到应用程序场景
- 《JavaFX应用程序添加HTML内容》-JavaFX WebView组件概述
- 《JavaFX应用程序添加HTML内容》-更新内容
- 《JavaFX应用程序添加HTML内容》-前言
- 《JavaFX应用程序添加HTML内容》-支持HTML5特性
- 《JavaFX应用程序添加HTML内容》-处理JavaScript命令
- 添加应用程序到启动器
- windows 添加应用程序到服务
- 添加应用程序到系统功能
- Ubuntu 添加应用程序到Launcher
- arm中添加应用程序到qtopia
- 手动把程序添加到应用程序里
- 在deepin中将快捷方式添加到应用程序
- 添加数据库文件到应用程序路径中
- 添加自己的应用程序到右键菜单
- Ubuntu 将应用程序添加到启动器
- ubuntu 12.04添加应用程序到Unity侧栏
- Ubuntu将应用程序添加到启动器
- 201612-3 权限查询
- 算法提高 8皇后·改
- POJ 1089 Intervals 笔记
- linux IO 内核参数调优 之 原理和参数介绍
- hibernate问题汇总
- 《JavaFX应用程序添加HTML内容》-添加WebView组件到应用程序场景
- leetcode-561-Array Partition I
- 2017/4/26阿里实习编程题:后缀表达式(Java实现)
- SSL 1589——传纸条
- CSS3动画属性2
- JavaScript学习笔记3-变量的声明和使用
- python爬虫之BeautifulSoup库
- java学习--求最小公约数和最大公倍数
- Python 3.6.1+Django 1.11+MySQL 5.7.18 开发环境搭建(mac os)(一)