我有一段时间没有写博客了,我想与其他人分享有关JavaFX的所有信息(我的日常工作和家庭可能是借口)。 对于那些是本博客的新手,我是JavaFX 2 Introduction by Example(JIBE)的作者, Java 7 Recipes的合著者以及Apress出版的Pro JavaFX 2书籍的技术审阅者。 对于那些已经认识我的人,我要感谢您通过购买这些书对我和其他作者的支持。 更重要的是,我希望与Java爱好者接触并分享想法。

他预定的JavaFX 2简介通过实例  于2011年11月发布,此后又添加了许多API。 在本书写作期间,我一直在研究JavaFX 2.0的早期版本,直到2011年10月JavaOne宣布为止。由于事情几乎是一成不变的,试图根据API的变化来更新本书非常疯狂。 我以为它竟然出​​了门真是太神奇了。 但是,我很高兴。 有些你们谁读的书(JIBE)的开始明白,JIBE的章节在书中Java 7的食谱也被找到(实际上它最初是从Java 7个食谱服用)。 这个事实说明了为什么JavaFX 2 Introduction by Example这本书让人想起食谱或食谱风格的技术书籍。 我的目的是帮助读者快速入门,而又无需太多技术问题。 与其尝试使人们相信JavaFX平台,不如通过有用的示例进行演示。 我发现,就一种特定技术为何优于另一种特定技术(俗气的80年代的Highlander参考文献 )进行深入的哲学辩论是没有用的。

压脚提升的JavaFX 2.0的发布,才出现了后来的版本,如JavaFX的2.1,2.2和JavaFX 8即将发布(2014年1月)。 在此博客文章中,我将提供JavaFX 8的Printing API的配方。 与我的书(JIBE)相似,我将遵循以前介绍问题,解决方案,代码和“工作原理”部分的相同模式。

声明 :在此博客中,您将遇到使用Lambda表达式的Java功能接口。 我将不在这里讨论它们,但将使您参考有关Project Lambda的 Oracle教程。

必备软件:

JDK 8 – https://jdk8.java.net/download.html

问题

您要创建一个JavaFX应用程序,以打印出访问过的网站。

使用JavaFX 8 PrintJobPrinter API打印任何JavaFX场景图节点。 另外,使用WebViewWebEngine API显示网站或网页。

使用说明

假设您已经编译并运行了该应用程序,请按照以下说明进行操作:

  1. 在文本字段中输入网站地址或网址。
  2. 按下回车键
  3. 页面加载后,单击“打印”按钮
  4. 转到打印机以获取打印的网页

package org.carlfx;import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.concurrent.Worker.State;
import javafx.print.*;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.transform.Scale;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;/*** Demo to use JavaFX 8 Printer API.** @author cdea*/
public class PrintDemo extends Application {@Overridepublic void start(Stage primaryStage) {final TextField urlTextField = new TextField();final Button printButton = new Button("Print");final WebView webPage = new WebView();final WebEngine webEngine = webPage.getEngine();HBox hbox = new HBox();hbox.getChildren().addAll(urlTextField, printButton);BorderPane borderPane = new BorderPane();borderPane.setTop(hbox);borderPane.setCenter(webPage);Scene scene = new Scene(borderPane, 300, 250);primaryStage.setTitle("Print Demo");primaryStage.setScene(scene);// print button pressed, page loadedfinal BooleanProperty printButtonClickedProperty = new SimpleBooleanProperty(false);final BooleanProperty pageLoadedProperty = new SimpleBooleanProperty(false);// when the a page is loaded and the button was pressed call the print() method.final BooleanProperty printActionProperty = new SimpleBooleanProperty(false);printActionProperty.bind(pageLoadedProperty.and(printButtonClickedProperty));// WebEngine updates flag when finished loading web page.webEngine.getLoadWorker().stateProperty().addListener( (ChangeListener) (obsValue, oldState, newState) -> {if (newState == State.SUCCEEDED) {pageLoadedProperty.set(true);}});// When user enters a url and hits the enter key.urlTextField.setOnAction( aEvent ->  {pageLoadedProperty.set(false);printButtonClickedProperty.set(false);webEngine.load(urlTextField.getText());});// When the user clicks the print button the webview node is printedprintButton.setOnAction( aEvent -> {printButtonClickedProperty.set(true);});// Once the print action hears a true go print the WebView node.printActionProperty.addListener( (ChangeListener) (obsValue, oldState, newState) -> {if (newState) {print(webPage);}});primaryStage.show();}/** Scales the node based on the standard letter, portrait paper to be printed.* @param node The scene node to be printed.*/public void print(final Node node) {Printer printer = Printer.getDefaultPrinter();PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();node.getTransforms().add(new Scale(scaleX, scaleY));PrinterJob job = PrinterJob.createPrinterJob();if (job != null) {boolean success = job.printPage(node);if (success) {job.endJob();}}}/*** The main() method is ignored in correctly deployed JavaFX application.* main() serves only as fallback in case the application can not be* launched through deployment artifacts, e.g., in IDEs with limited FX* support. NetBeans ignores main().** @param args the command line arguments*/public static void main(String[] args) {launch(args);}
}

使用JavaFX 8进行打印演示

怎么运行的

他代码首先创建一个TextField,按钮,和一个WebView控件被放置到一个BorderPane。 使用BorderPane布局时,您可以将控件放置在以下区域中:顶部,右侧,左侧,底部和中心。

与网络浏览器类似,文本字段允许用户输入网站网址。 输入URL后,用户将按Enter键将网页加载到WebView节点中。 将控件放置在任何侧面区域上时,BorderPane布局将采用添加的所有控件的首选高度。 中心区域将允许节点占用可用空间减去边界侧边区域的宽度和高度所占用的剩余空间。 换句话说,如果侧面区域不包含节点(空),则位于中心区域的节点将有机会占用其父级(场景)提供的所有可用宽度和高度空间。 由于WebView节点将占据中心区域,因此在完全加载网页后,它将占据所有可用的宽度和高度(减去顶部区域)。 您还将注意到滚动条,该滚动条允许用户查看大于当前查看端口的页面。

布置完UI的所有组件后,您需要进行连接。 在这里,您将简单地创建三个布尔属性
(javafx.beans.property.SimpleBooleanProperty)实例。 第一个属性变量printButtonClickedProperty是一个标志,指示何时单击打印按钮。 第二个属性pageLoadedProperty是一个标志,指示该网页已完成加载。 最后,您将需要注意printActionProperty ,它通过使用流畅的API绑定了printButtonClickedProperty和pageLoadedProperty。 当他们评估时,如果printLoadedProperty和printLoadedProperty均为真值,则printActionProperty将为true。

// print button pressed, page loaded
final BooleanProperty printButtonClickedProperty = new SimpleBooleanProperty(false);
final BooleanProperty pageLoadedProperty = new SimpleBooleanProperty(false);// when the a page is loaded and the button was pressed call the print() method.
final BooleanProperty printActionProperty = new SimpleBooleanProperty(false);
printActionProperty.bind(pageLoadedProperty.and(printButtonClickedProperty));

çontinuing接线了UI的,我花了一个事件驱动的方法,把处理程序代码将事件和性能的变化。 从WebView节点开始,我将处理程序代码附加到statePropery实例(ChangeListener),以便在成功加载网页后将pageLoadedProperty设置为true。

// WebEngine updates flag when finished loading web page.webEngine.getLoadWorker().stateProperty().addListener( (ChangeListener) (obsValue, oldState, newState) -> {if (newState == State.SUCCEEDED) {pageLoadedProperty.set(true);}});

ñ分机,你会看到一个包含处理程序代码,复位pageLoadedPropertyprintButtonClickedProperty对象的文本字段的“setOnAction”的方法。 同样,代码将通过WebViewWebEngine load()方法启动页面的加载。

// When user enters a url and hits the enter key.urlTextField.setOnAction( aEvent ->  {pageLoadedProperty.set(false);printButtonClickedProperty.set(false);webEngine.load(urlTextField.getText());});

压脚提升TextField的控制的动作代码是有线了,打印按钮,还需要处理程序代码到printButtonClickedProperty标志设置为true。 最后,当printActionProperty属性的状态评估为true时,它需要一个ChangeListener来响应。 当此评估为true时,将调用我的print()方法。

// When the user clicks the print button the webview node is printedprintButton.setOnAction( aEvent -> {printButtonClickedProperty.set(true);});// Once the print action hears a true go print the WebView node.printActionProperty.addListener( (ChangeListener) (obsValue, oldState, newState) -> {if (newState) {print(webPage);}});

˚Finally,打印()方法采用要被打印的JavaFX的节点对象。 Printer对象具有一种方法,该方法返回计算机设置为的默认打印机。 在实际打印之前,我们可以导出默认页面布局以在打印节点之前缩放节点。 如果不这样做,将仅打印部分网页。 获得默认打印机后,将调用createPrinterJob()方法以返回执行实际打印的PrinterJob实例。 要打印JavaFX可显示类型的节点,只需将Node实例作为参数传入即可调用PrinterJob对象的printPage()方法。

/** Scales the node based on the standard letter, portrait paper to be printed.* @param node The scene node to be printed.*/public void print(final Node node) {Printer printer = Printer.getDefaultPrinter();PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();node.getTransforms().add(new Scale(scaleX, scaleY));PrinterJob job = PrinterJob.createPrinterJob();if (job != null) {boolean success = job.printPage(node);if (success) {job.endJob();}}}

I N最后,我发现,API是简单相比的Java Swing / AWT API来使用。 我想提一下,您可以使用许多功能,因为此博客条目仅涉及当前可用API的表面。

注意:JavaFX 8打印机API仍处于早期阶段,仍然存在未解决的问题(Jira问题)。

参考: 示例介绍:JavaFX 8由我们的JCG合作伙伴 Carl Dea在Carl's FX Blog博客上进行打印 。

翻译自: https://www.javacodegeeks.com/2013/07/introduction-by-example-javafx-8-printing.html

示例介绍:JavaFX 8打印相关推荐

  1. javafx 示例_示例介绍:JavaFX 8打印

    javafx 示例 我有一段时间没有写博客了,我想与其他人分享有关JavaFX的所有信息(我的日常工作和家庭可能是借口). 对于那些对此博客不熟悉的人 ,我是JavaFX 2 Introduction ...

  2. javafx 教程_示例介绍:JavaFX 8打印

    javafx 教程 我有一段时间没有写博客了,我想与其他人分享有关JavaFX的所有信息(我的日常工作和家庭可能是借口). 对于那些是本博客的新手,我是JavaFX 2 Introduction by ...

  3. 书评:JavaFX 2.0:示例介绍

    尽管Oracle在JavaOne 2010和JavaOne 2011上对JavaFX的更改使我从怀疑论者转变为对JavaFX的信奉者 ,但是JavaFX愿景的转变并非没有缺点 . 特别是,JavaFX ...

  4. 索引扫描、查找、书签查询、覆盖查询示例介绍

    真正理解查询计划和发现与计划有关的问题,需要对构成这些计划的查询运算符有深入的了解.总而言之,有许多运算符值得探讨. 扫描与查找 扫描与查找操作均是SQL Server从表或索引中读取数据采用的迭代器 ...

  5. 快嘉开发框架1.0和示例介绍及使用说明

    2019独角兽企业重金招聘Python工程师标准>>> 快嘉开发框架1.0和示例介绍 快嘉开发框架将一个标准的J2EE项目拆分为基础组件(Base).持久层组件(Persistenc ...

  6. PyG图神经网络框架学习--示例介绍

    实例介绍 通过自带的示例介绍并学习PyG.主要从以下4各方面: 图数据处理 通用基准数据集 Mini-batches 数据转换 图学习方法 图数据处理 图用于对对象(节点)之前的关系(边)进行建模.P ...

  7. ASP.NET Web API 开篇示例介绍

    ASP.NET Web API 开篇示例介绍 ASP.NET Web API 对于我这个初学者来说ASP.NET Web API这个框架很陌生又熟悉着. 陌生的是ASP.NET Web API是一个全 ...

  8. java输出华氏摄氏温度转换表_C语言入门教程-示例:编写能够打印华氏-摄氏温度转换表的程序...

    示例:编写能够打印华氏-摄氏温度转换表的程序 假设您要编写一个能够打印华氏-摄氏温度转换表的程序.可以使用for或者while循环可以轻松实现: #include int main() {int a; ...

  9. Linux shell命令示例介绍-个人笔记

    转载: https://blog.csdn.net/zhangwu1241/article/details/60871000 Linux中命令示例介绍-个人笔记 为了方便以后查阅,会对在Linux学习 ...

最新文章

  1. maven 如何看jar是否被修改_如何在线修改jar文件
  2. 如何将数据导入到 SQL Server Compact Edition 数据库中(四)
  3. 整理一周的Python全品类资料包含100本电子书,还有独家实战项目源码公开!
  4. springboot2整合mysql5_SpringBoot2整合SSM框架详解
  5. httphost设置socks_socks5代理使用和安装配置
  6. linux核心设计ebpf,Linux eBPF介绍
  7. POJ 2289 Jamie's Contact Groups 【二分】+【多重匹配】(模板题)
  8. Bloomberg 的一些功能
  9. 为什么 jsp 还没有淘汰?
  10. 【业余无线电BI1FKP】万里通UV5F-Plus写频、自制写频线
  11. 基于 VIVADO 的 AM 调制解调(1)方案设计
  12. TAT-QA: A Question Answering Benchmark on a Hybrid of Tabular and Textual Content in Finance
  13. SpringCloudAlibaba——Nacos实现原理详解
  14. 如何解决安装或者卸载时 临时文件夹已满或不能访问
  15. Bit Twiddling Hacks
  16. ajax请求 下载zip压缩包
  17. 计算机科学与技术专业软考考什么,计算机专业软考考什么
  18. vmware虚拟机无法u盘启动的解决办法
  19. Vue ajax跨域请求*
  20. 职称计算机考试输入破折号,2015职称计算机考试Dreamweaver考前测试题及答案

热门文章

  1. Linux ss命令 报错,ECS Linux中ss命令显示连接状态的使用说明
  2. 计量经济学自相关matlab,计量经济学 自相关性.ppt
  3. 数据库表连接总结:等值连接, 自然连接,左外连接,右外连接,内连接,全外连接;
  4. springmvc(18)使用WebSocket 和 STOMP 实现消息功能
  5. jpa避免n+1_JPA技巧:避免N + 1选择问题
  6. java超出gc开销_通过这5个简单的技巧减少GC开销
  7. web.xml.jsf_看一下即将发布的JSF 2.3 Push支持
  8. 将应用程序集成为Heroku附加组件
  9. Derby数据库备份
  10. 远程桌面服务怎么测试_快速的远程服务测试