日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2023/12/3 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javafx 示例_示例介绍:JavaFX 8打印 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

javafx 示例

我有一段時(shí)間沒(méi)有寫(xiě)博客了,我想與其他人分享有關(guān)JavaFX的所有信息(我的日常工作和家庭可能是借口)。 對(duì)于那些對(duì)此博客不熟悉的人 ,我是JavaFX 2 Introduction by Example(JIBE)的作者, Java 7 Recipes的合著者,以及Apress出版的Pro JavaFX 2書(shū)籍的技術(shù)審閱者。 對(duì)于那些已經(jīng)認(rèn)識(shí)我的人,我要感謝您通過(guò)購(gòu)買(mǎi)這些書(shū)對(duì)我和其他作者的支持。 更重要的是,我希望與Java愛(ài)好者接觸并分享想法。

噸他預(yù)定的JavaFX 2簡(jiǎn)介通過(guò)實(shí)例 , ? 于2011年11月發(fā)布,此后又添加了許多API。 在本書(shū)寫(xiě)作期間,我一直在研究JavaFX 2.0的早期版本,直到2011年10月JavaOne宣布為止。由于事情幾乎是一成不變的,試圖根據(jù)API的變化來(lái)更新本書(shū)非常瘋狂。 我以為它竟然走出了門(mén)真是太神奇了。 但是,我很高興。 有些你們誰(shuí)讀的書(shū)(JIBE)的開(kāi)始明白,JIBE的章節(jié)在書(shū)中Java 7的食譜也被找到(實(shí)際上它最初是從Java 7個(gè)食譜服用)。 這個(gè)事實(shí)說(shuō)明了為什么JavaFX 2 Introduction by Example這本書(shū)讓人想起食譜或食譜風(fēng)格的技術(shù)書(shū)籍。 我的目的是幫助讀者快速入門(mén),而又無(wú)需太多技術(shù)問(wèn)題。 與其嘗試使人們相信JavaFX平臺(tái),不如通過(guò)有用的示例進(jìn)行演示。 我發(fā)現(xiàn)在深入探討關(guān)于某項(xiàng)特定技術(shù)為何優(yōu)于另一項(xiàng)特定技術(shù)方面的哲學(xué)辯論時(shí),會(huì)適得其反(俗氣的80年代Highlander參考資料 )。

壓腳提升的JavaFX 2.0的發(fā)布,才出現(xiàn)了后來(lái)的版本,如JavaFX的2.1,2.2和JavaFX 8即將發(fā)布(2014年1月)。 在此博客文章中,我將提供JavaFX 8的Printing API的配方。 與我的書(shū)(JIBE)相似,我將遵循以前介紹問(wèn)題,解決方案,代碼和“工作原理”部分的相同模式。

聲明 :在此博客中,您將遇到使用Lambda表達(dá)式的Java功能接口。 我將不在這里討論它們,但將使您參考Oracle關(guān)于Lambda的教程。

必備軟件:

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

問(wèn)題

您要?jiǎng)?chuàng)建一個(gè)JavaFX應(yīng)用程序,以打印出訪(fǎng)問(wèn)過(guò)的網(wǎng)站。

使用JavaFX 8 PrintJobPrinter API打印任何JavaFX場(chǎng)景圖節(jié)點(diǎn)。 另外,使用WebViewWebEngine API顯示網(wǎng)站或網(wǎng)頁(yè)。

使用說(shuō)明

假設(shè)您已經(jīng)編譯并運(yùn)行了應(yīng)用程序,請(qǐng)按照以下說(shuō)明進(jìn)行操作:

  • 在文本字段中輸入網(wǎng)站地址或網(wǎng)址。
  • 按下回車(chē)鍵
  • 頁(yè)面加載后,單擊“打印”按鈕
  • 轉(zhuǎn)到打印機(jī)以獲取打印的網(wǎng)頁(yè)
  • 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進(jìn)行打印演示

    這個(gè)怎么運(yùn)作

    代碼首先創(chuàng)建一個(gè)TextField ,一個(gè)Button和一個(gè)WebView控件以放置到BorderPane中 。 使用BorderPane布局時(shí),您將能夠?qū)⒖丶胖迷谝韵聟^(qū)域中:頂部,右側(cè),左側(cè),底部和中心。

    與網(wǎng)絡(luò)瀏覽器類(lèi)似,文本字段允許用戶(hù)輸入網(wǎng)站網(wǎng)址。 輸入U(xiǎn)RL后,用戶(hù)將按Enter鍵將網(wǎng)頁(yè)加載到WebView節(jié)點(diǎn)中。 將控件放置在任何側(cè)面區(qū)域上時(shí),BorderPane布局將采用所添加的任何控件的首選高度。 中心區(qū)域?qū)⒃试S節(jié)點(diǎn)占用可用空間減去邊界側(cè)邊區(qū)域的寬度和高度所占用的剩余空間。 換句話(huà)說(shuō),如果側(cè)面區(qū)域不包含節(jié)點(diǎn)(空),則中心區(qū)域中的節(jié)點(diǎn)將有機(jī)會(huì)占用其父級(jí)(場(chǎng)景)提供的所有可用寬度和高度空間。 由于WebView節(jié)點(diǎn)將占據(jù)中心區(qū)域,因此在完全加載網(wǎng)頁(yè)后,它將占據(jù)所有可用的寬度和高度(減去頂部區(qū)域)。 您還會(huì)注意到滾動(dòng)條,該滾動(dòng)條允許用戶(hù)查看大于當(dāng)前查看端口的頁(yè)面。

    布置完UI的所有組件后,您需要進(jìn)行連接。 在這里,您將簡(jiǎn)單地創(chuàng)建三個(gè)布爾屬性
    (javafx.beans.property.SimpleBooleanProperty)實(shí)例。 第一個(gè)屬性變量printButtonClickedProperty是一個(gè)標(biāo)志,指示何時(shí)單擊打印按鈕。 第二個(gè)屬性pageLoadedProperty是一個(gè)標(biāo)志,指示該網(wǎng)頁(yè)已完成加載。 最后,您需要注意printActionProperty ,它通過(guò)使用流暢的API綁定了printButtonClickedProperty和pageLoadedProperty。 當(dāng)他們?cè)u(píng)估時(shí),如果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接線(xiàn)了UI的,我花了一個(gè)事件驅(qū)動(dòng)的方法,把處理程序代碼將事件和性能的變化。 從WebView節(jié)點(diǎn)開(kāi)始,我將處理程序代碼附加到statePropery實(shí)例(ChangeListener),以便在成功加載網(wǎng)頁(yè)后將pageLoadedProperty設(shè)置為true。

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

    ?分機(jī),你會(huì)看到一個(gè)包含處理程序代碼,復(fù)位pageLoadedProperty和printButtonClickedProperty對(duì)象的文本字段的“setOnAction”的方法。 同樣,代碼將通過(guò)WebView的WebEngine load()方法啟動(dòng)頁(yè)面的加載。

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

    壓腳提升TextField的控制的動(dòng)作代碼是有線(xiàn)了,打印按鈕,還需要處理程序代碼到printButtonClickedProperty標(biāo)志設(shè)置為true。 最后,當(dāng)printActionProperty屬性的狀態(tài)評(píng)估為true時(shí),它需要一個(gè)ChangeListener來(lái)響應(yīng)。 當(dāng)此評(píng)估為true時(shí),將調(diào)用我的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的節(jié)點(diǎn)對(duì)象。 Printer對(duì)象具有一種方法,該方法返回計(jì)算機(jī)設(shè)置為的默認(rèn)打印機(jī)。 在實(shí)際打印之前,我們可以導(dǎo)出默認(rèn)頁(yè)面布局以在打印節(jié)點(diǎn)之前縮放節(jié)點(diǎn)。 如果您不這樣做,則只會(huì)打印部分網(wǎng)頁(yè)。 獲得默認(rèn)打印機(jī)后,將調(diào)用createPrinterJob()方法以返回執(zhí)行實(shí)際打印的PrinterJob實(shí)例。 要打印JavaFX可顯示類(lèi)型的節(jié)點(diǎn),只需將Node實(shí)例作為參數(shù)傳入即可調(diào)用PrinterJob對(duì)象的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最后,我發(fā)現(xiàn),API是簡(jiǎn)單相比的Java Swing / AWT API來(lái)使用。 我想提一下,您可以使用許多功能,因?yàn)榇瞬┛蜅l目?jī)H涉及當(dāng)前可用API的表面。

    注意:JavaFX 8打印機(jī)API仍處于早期階段,仍然存在未解決的問(wèn)題(Jira問(wèn)題)。

    參考: 示例介紹:JavaFX 8由我們的JCG合作伙伴 Carl Dea在Carl's FX Blog博客上進(jìn)行打印 。

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

    javafx 示例

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的javafx 示例_示例介绍:JavaFX 8打印的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。