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;
    }
}

 

 

原文地址:https://blog.csdn.net/zy103118/article/details/127391782

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_22986.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注