示例介绍:JavaFX 8打印
我有一段時間沒有寫博客了,我想與其他人分享有關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 PrintJob和Printer API打印任何JavaFX場景圖節點。 另外,使用WebView和WebEngine API顯示網站或網頁。
使用說明
假設您已經編譯并運行了該應用程序,請按照以下說明進行操作:
碼
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。
?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);}});?分機,你會看到一個包含處理程序代碼,復位pageLoadedProperty和printButtonClickedProperty對象的文本字段的“setOnAction”的方法。 同樣,代碼將通過WebView的WebEngine 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問題)。
翻譯自: https://www.javacodegeeks.com/2013/07/introduction-by-example-javafx-8-printing.html
總結
以上是生活随笔為你收集整理的示例介绍:JavaFX 8打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电视端可用:B站超级大会员年卡 128
- 下一篇: Spring MVC自定义验证注释