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;
/**
* @author zhaoyong
* @Date 2022/10/18
* @Description
*/
public class WebViewTest 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"));//场景上挂载浏览器对象new Browser()
stage.setScene(scene);
scene.getStylesheets().add("BrowserToolbar.css");//场景对象引用外部css文件
// show stage
stage.show();//舞台展现
}
public static void main(String[] args) {
launch(args);
}
}
_______________________________________________
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
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.web.WebEngine;
import javafx.scene.web.WebView;
class Browser extends Region {
private HBox toolBar;
final private static String[] imageFiles = new String[]{//图片路径数组
"folder_16.jpg",
"folder_16.jpg",
"root.jpg",
"root.jpg"
};
final private static String[] captions = new String[]{//标题名称数组
"Products",
"Blogs",
"Documentation",
"Partners"
};
final private static String[] urls = new String[]{//添加的测试url集合
"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];//具体url的读取
hpl.setOnAction(new EventHandler<ActionEvent>() {//超级链接对象添加点击处理事件处理回调函数机制的编写
@Override
public void handle(ActionEvent e) {
webEngine.load(url);//让浏览器内核加载url指向的资源
}
});
}
// load the home page
webEngine.load("http://www.oracle.com/products/index.html");//浏览器引擎初始化url加载
// 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() {//浏览器页面初始化页面样式加载重写父类Region对象同名方法
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) {//浏览器创后宽度设置重写了父类Region对象同名方法
return 900;
}
@Override
protected double computePrefHeight(double width) {
return 600;
}
}
该博客展示了如何使用JavaFX库创建一个简单的网页浏览器应用。应用包含一个工具栏,展示多个链接,如产品、博客、文档和合作伙伴,每个链接都配有一个图标,点击链接可以在内置的WebView中加载相应的网页。通过WebEngine加载URL实现页面浏览功能。

3073

被折叠的 条评论
为什么被折叠?



