当前位置: 首页 > news >正文

JavaFX之WebView超详细解析!

JavaFX WebView

JavaFX教程 - JavaFX WebView

JavaFX提供了一个GUI WebView(javafx.scene.web.WebView)节点,以将HTML5内容呈现到场景图形上。

WebView节点是一个小型浏览器。

我们可以使用以下代码加载网页并显示它。

WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("http://mySite.com");

完整的源代码

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());


    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    webEngine.getLoadWorker().stateProperty()
        .addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue ov, State oldState, State newState) {

            if (newState == Worker.State.SUCCEEDED) {
              stage.setTitle(webEngine.getLocation());
            }

          }
        });
    webEngine.load("http://www.w3cschool.cn");

    scene.setRoot(scrollPane);

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

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

上面的代码生成以下结果。

null

WebEvents

JavaFX Web API也遵循事件驱动的编程模型。

网页中的以下JavaScript将弹出一个包含浏览器窗口消息的警报对话框。

<script>
alert("JavaFX is  Awesome");
</script>

当在JavaFX WebView节点中执行代码时,不会弹出本机对话框窗口。但是,OnAlert事件作为javafx.scene.web.WebEvent对象引发。

我们可以处理这些事件。要设置处理程序,请使用带有类型为WebEvent的入站参数的setOnAlert()方法。

    browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
      System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });

完整的源代码

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
      System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });

    webEngine.load("http://www.w3cschool.cn");

    scene.setRoot(scrollPane);

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

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

在事件处理程序中,我们可以决定是否显示一个对话框。

下表显示了从javafx.scene.web.WebEngine WebEvents和Properties触发它们的事件和操作。

SetOn方法方法属性描述
setOnAlert()onAlertProperty()处理JavaScript警报方法
setOnError()onErrorProperty()WebEngine错误处理程序
setOnResized()onResizedProperty()JavaScript调整大小处理程序
setOnStatusChanged()onStatusChanged()JavaScript状态处理程序
setOnVisibilityChanged()onVisibilityChangedProperty()JavaScript窗口可见处理程序
setConfirmHandler()confirmHandlerProperty()JavaScript确认窗口

上面的代码生成以下结果。

null

管理网络历史记录

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebHistory.Entry;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
      System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });

    webEngine.load("http://www.w3cschool.cn");

    
    final WebHistory history = webEngine.getHistory();
    history.getEntries().addListener(new 
        ListChangeListener<WebHistory.Entry>() {
            @Override
            public void onChanged(Change<? extends Entry> c) {
                c.next();
                for (Entry e : c.getRemoved()) {
                    System.out.println(e.getUrl());
                }
                for (Entry e : c.getAddedSubList()) {
                    System.out.println(e.getUrl());
                }
            }
        }
    );
             
    history.go(0);
    scene.setRoot(scrollPane);

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

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

上面的代码生成以下结果。

null

打印HTML内容

以下代码显示如何从WebEngine进行打印。

    PrinterJob job = PrinterJob.createPrinterJob();
    if (job != null) {
        webEngine.print(job);
        job.endJob();
    }

完整的源代码

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.print.PrinterJob;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    VBox root = new VBox();

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    webEngine.getLoadWorker().stateProperty()
        .addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SUCCEEDED) {
              PrinterJob job = PrinterJob.createPrinterJob();
              if (job != null) {
                webEngine.print(job);
                job.endJob();
              }
            }
          }
        });
    webEngine.load("http://www.w3cschool.cn");

    root.getChildren().addAll(scrollPane);
    scene.setRoot(root);

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

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

使用WebView显示HTML

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

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTML");
        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();

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(browser);
        webEngine.loadContent("<b>asdf</b>");

        root.getChildren().addAll(scrollPane);
        scene.setRoot(root);

        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

使用CSS更改WebView背景

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

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTML");
        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();
        
     
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setStyle("-fx-background-color: white");
        
        scrollPane.setContent(browser);
        webEngine.loadContent("<b>asdf</b>");
         
        
        root.getChildren().addAll(scrollPane);
        scene.setRoot(root);
 
        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

相关文章:

  • javafx之webEngine超详细解析
  • mybatis-plus进阶教程!超详细使用方法解析!
  • mybatis-plus入门教程!看完必懂!超详细解析!
  • Mybatis-Plus中的代码生成器AutoGenerator超详细解析!完整配置!
  • Mybatis plus关闭驼峰命名,防止出现查询为Null!四种方法超详细解析!
  • Spring体系结构超详细解析!
  • 自然语言处理系列之: NLP基础
  • 自然语言处理系列之:中文分词技术
  • 自然语言处理系列之:词性标注与命名实体识别
  • 自然语言处理系列之: 关键词提取算法
  • 自然语言处理系列之: 句法分析
  • 自然语言处理系列之:文本向量化
  • 自然语言处理系列之: 实战电影评论情感分析
  • 自然语言处理系列之: NLP中用到的机器学习算法
  • Java网络编程:UDP套接字程序设计,UDP实现Socket通信(附完整代码实现)
  • Angularjs之国际化
  • Fabric架构演变之路
  • Iterator 和 for...of 循环
  • java中的hashCode
  • Linux Process Manage
  • magento 货币换算
  • Sass 快速入门教程
  • SpringBoot几种定时任务的实现方式
  • Web设计流程优化:网页效果图设计新思路
  • 程序员最讨厌的9句话,你可有补充?
  • 基于webpack 的 vue 多页架构
  • 京东美团研发面经
  • 前端临床手札——文件上传
  • 新书推荐|Windows黑客编程技术详解
  • 一个JAVA程序员成长之路分享
  • 怎么将电脑中的声音录制成WAV格式
  • 从如何停掉 Promise 链说起
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​插件化DPI在商用WIFI中的价值
  • ​人工智能书单(数学基础篇)
  • #define 用法
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (function(){})()的分步解析
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (第61天)多租户架构(CDB/PDB)
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (五)关系数据库标准语言SQL
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (译) 函数式 JS #1:简介
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)Scala的“=”符号简介
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .netcore如何运行环境安装到Linux服务器