溫馨提示×

使用JavaFX打印的方法是什么

小億
178
2024-04-28 12:55:41
欄目: 編程語言

在JavaFX中,可以使用PrinterJob類來實現打印功能。下面是一個簡單的示例代碼,演示如何在JavaFX應用程序中打?。?/p>

import javafx.application.Application;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button printButton = new Button("Print");
        printButton.setOnAction(event -> {
            PrinterJob printerJob = PrinterJob.createPrinterJob();
            if (printerJob != null && printerJob.showPrintDialog(primaryStage)) {
                Node node = new Button("Printed Content");
                VBox root = new VBox(node);
                Scene scene = new Scene(root, 200, 200);
                printerJob.printPage(node);
                printerJob.endJob();
            }
        });

        VBox root = new VBox(printButton);
        Scene scene = new Scene(root, 400, 400);

        primaryStage.setTitle("Print Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

在上面的示例中,創建了一個簡單的JavaFX應用程序,包含一個按鈕,當點擊按鈕時,會彈出打印對話框,并打印按鈕上的文字內容。通過PrinterJob類的printPage方法可以實現打印操作。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女