参考的原文链接DOC-04-08 管理Web历史记录 | JavaFX中文资料
help.html 的代码
<html lang="en"> <head> <!-- Visibility toggle script --> <script type="text/javascript"> <!-- function toggle_visibility(id) { var e = document.getElementById(id); if (e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } //--> </script> </head> <body> <h1>Online Help</h1> <p class="boxtitle"><a href="#" οnclick="toggle_visibility('help_topics');" class="boxtitle">[+] Show/Hide Help Topics</a></p> <ul id="help_topics" style='display:none;'> <li>Products - Extensive overview of Oracle hardware and software products, and summary Oracle consulting, support, and educational services. </li> <li>Blogs - Oracle blogging community (use the Hide All and Show All buttons to collapse and expand the list of topics).</li> <li>Documentation - Landing page to start learning Java. The page contains links to the Java tutorials, developer guides, and API documentation.</li> <li>Partners - Oracle partner solutions and programs. Popular resources and membership opportunities.</li> </ul> </body> </html>
________________
import javafx.application.Platform; /** * @author zhaoyong * @Date 2022/10/20 * @Description */ public class JavaApp { public void exit() { Platform.exit(); } }
________________
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
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.WebHistory;
import javafx.scene.web.WebView;
import javafx.concurrent.Worker.State;
import netscape.javascript.JSObject;
import javafx.scene.web.PopupFeatures;
class Browser extends Region {
private HBox toolBar;
final private static String[] imageFiles = new String[]{//图片路径数组
"folder_16.jpg",
"folder_16.jpg",
"root.jpg",
"root.jpg",
"root.jpg"
};
final private static String[] captions = new String[]{//标题名称数组
"百度",
"Blogs",
"Documentation",
"Partners",
"Help"
};
final private static String[] urls = new String[]{//添加的测试url集合
"http://www.baidu.com",
"http://blogs.oracle.com/",
"http://docs.oracle.com/javase/index.html",
"http://www.oracle.com/partners/index.html",
Browser.class.getResource("help.html").toExternalForm()
};
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();//获取浏览器引擎
final Button toggleHelpTopics = new Button("Toggle Help Topics");
final ComboBox comboBox = new ComboBox();
private boolean needDocumentationButton = false;
final WebView smallView = new WebView();
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的读取
final boolean addButton = (hpl.getText().equals("Help"));
hpl.setOnAction((ActionEvent e) -> {//超级链接对象添加点击处理事件处理回调函数机制的编写
needDocumentationButton = addButton;
webEngine.load(url);//让浏览器内核加载url指向的资源
});
}
// create the toolbar
toolBar = new HBox();
toolBar.setAlignment(Pos.CENTER);
toolBar.getStyleClass().add("browser-toolbar");//水平布局盒子对象的外部样式引入
toolBar.getChildren().addAll(hpls);//水平布局盒子上面添加超级链接对象
toolBar.getChildren().add(comboBox);//创建comboBox
toolBar.getChildren().add(createSpacer());//创建一个空白区域
//set action for the button
toggleHelpTopics.setOnAction((ActionEvent t) -> {//让浏览器内核执行js代码,本例中是调用javascript函数
webEngine.executeScript("toggle_visibility('help_topics')");
});
//add components
smallView.setPrefSize(300, 200);
//handle popup windows
webEngine.setCreatePopupHandler(
(PopupFeatures config) -> {
smallView.setFontScale(0.8);
if (!toolBar.getChildren().contains(smallView)) {
toolBar.getChildren().add(smallView);
}
return smallView.getEngine();
});
// process page loading
webEngine.getLoadWorker().stateProperty().addListener(
(ObservableValue<? extends State> ov, State oldState,
State newState) -> {
toolBar.getChildren().remove(toggleHelpTopics);
if (newState == State.SUCCEEDED) {
JSObject win
= (JSObject) webEngine.executeScript("window");//获取浏览器引擎中的javascript语言中的window全局对象
win.setMember("app", new JavaApp());//创建一个全局对象app(类型是前文中自定义的JavaApp类型),将此app对象置入浏览器引擎中的window对象体内(window对象上下文环境中)
if (needDocumentationButton) {
toolBar.getChildren().add(toggleHelpTopics);
}
}
});
// load the home page
webEngine.load("http://www.oracle.com/products/index.html");//浏览器引擎初始化url加载
final WebHistory history = webEngine.getHistory();
history.getEntries().addListener(new ListChangeListener<WebHistory.Entry>() {
@Override
public void onChanged(Change<? extends WebHistory.Entry> c) {
c.next();
for (WebHistory.Entry e : c.getRemoved()) {
comboBox.getItems().remove(e.getUrl());
}
for (WebHistory.Entry e : c.getAddedSubList()) {
comboBox.getItems().add(e.getUrl());
}
}
});
comboBox.setPrefWidth(60);
comboBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ev) {
int offset = comboBox.getSelectionModel().getSelectedIndex() - history.getCurrentIndex();
history.go(offset);
}
});
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; } }
____________________
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); } }