You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaFX中监控打印时长?含示例代码实现咨询

在JavaFX中监控打印操作时长的方案

嘿,我来帮你搞定JavaFX里打印时长监控的问题!针对你的两个疑问,我分两部分解答:

一、如何监控单次打印操作的时长

JavaFX的打印核心类是PrinterJob,我们可以通过记录时间戳+监听打印状态/捕获打印结果的方式来计算时长,具体有两种常用方案:

方案1:同步打印前后直接计时

因为PrinterJob的打印方法(printPage()、endJob())是同步执行的,所以可以在打印开始前记录起始时间,打印完成(或失败)后记录结束时间,两者差值就是打印时长。这种方式简单直接,适合大多数场景:

import javafx.application.Application;
import javafx.print.PrinterJob;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class PrintDurationMonitor extends Application {
    @Override
    public void start(Stage primaryStage) {
        PrinterJob printerJob = PrinterJob.createPrinterJob();
        if (printerJob == null) {
            System.out.println("Failed to create printer job");
            return;
        }

        // 弹出打印对话框,用户确认后开始打印
        boolean userConfirmed = printerJob.showPrintDialog(primaryStage);
        if (userConfirmed) {
            long startTime = System.currentTimeMillis();
            try {
                // 打印内容(这里用简单的Text示例,你可以替换成任意Node)
                boolean printSuccess = printerJob.printPage(new Text("JavaFX Print Test Content"));
                if (printSuccess) {
                    printerJob.endJob(); // 结束打印任务
                    long endTime = System.currentTimeMillis();
                    long duration = endTime - startTime;
                    System.out.println("Printing Duration: " + duration + " ms");
                } else {
                    System.out.println("Printing failed: Page print unsuccessful");
                }
            } catch (Exception e) {
                long endTime = System.currentTimeMillis();
                long duration = endTime - startTime;
                System.out.println("Printing failed after " + duration + " ms: " + e.getMessage());
                e.printStackTrace();
            }
        } else {
            System.out.println("Print job cancelled by user");
        }
    }

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

方案2:监听打印任务状态变化

如果你需要更精细地捕捉打印的各个阶段(比如开始打印、打印完成、取消、失败),可以监听PrinterJob的jobStatusProperty(),在状态切换时记录时间:

printerJob.jobStatusProperty().addListener((observable, oldStatus, newStatus) -> {
    long startTime;
    switch (newStatus) {
        case PRINTING:
            startTime = System.currentTimeMillis();
            System.out.println("Printing started");
            break;
        case DONE:
            long endTime = System.currentTimeMillis();
            System.out.println("Printing Duration: " + (endTime - startTime) + " ms");
            break;
        case CANCELED:
            long cancelTime = System.currentTimeMillis();
            System.out.println("Printing cancelled after " + (cancelTime - startTime) + " ms");
            break;
        case FAILED:
            long failTime = System.currentTimeMillis();
            System.out.println("Printing failed after " + (failTime - startTime) + " ms");
            break;
        default:
            // 其他状态如READY、NOT_STARTED等,按需处理
            break;
    }
});

二、关于用duration++方式监控时长的可行性

你提到的System.out.println("Printing Duration: " + duration++);这种写法,如果是用来累计多次打印的总时长是可行的,但单次打印时长不能直接用自增:

  • 如果是单次打印:直接计算结束时间-开始时间即可,自增只会让数值每次加1,无法反映真实时长。
  • 如果是累计多次打印的总时长:可以维护一个全局的累计变量,每次打印完成后把本次时长加到变量上,示例如下:
// 全局累计时长变量
private static long totalPrintDuration = 0;

// 在单次打印完成后:
long currentDuration = endTime - startTime;
totalPrintDuration += currentDuration;
System.out.println("Total Printing Duration: " + totalPrintDuration + " ms");

这样每次打印后,totalPrintDuration会累计所有打印操作的时长,符合你想要的“累计监控”需求。


内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:44:34