示例介绍:JavaFX 8打印

本文介绍了如何使用JavaFX 8的PrintJob和Printer API来打印JavaFX场景图节点,展示了一个显示和打印网页的示例。文章还提到了Lambda表达式的使用,并指出JavaFX 8的打印API相比Swing/AWT更易于使用,尽管仍存在一些待解决的问题。
摘要由CSDN通过智能技术生成

我有一段时间没有写博客了,我想与其他人分享有关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 {
    @Override
    public 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 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));

        // 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 printed
        printButton.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 printed
        printButton.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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值