我想在打印30份(在打印机上逐字打印)完成后再暂停约10分钟并再次执行。
public class printImage {
static public void main(String args[]) throws Exception {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(30));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
PrintService ps = pss[4];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("mypic.JPG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
fin.close();
}
}
答案 0 :(得分:0)
您似乎需要检查PrintJobListener界面。
此侦听器接口的实现应附加到a DocPrintJob用于监控打印机作业的状态。这些回调 可以在处理打印作业的线程上调用方法,或者 服务创建通知线程。无论哪种情况,客户都应该 在这些回调中不执行冗长的处理。
您可以将代码修改为:
public static void main(String[] args) throws IOException, PrintException {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(30));
PrintService pss[] = (PrintService[]) PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
PrintService ps = pss[4];
System.out.println("Printing to " + ps);
DocPrintJob job = pss[0].createPrintJob();
FileInputStream fin = new FileInputStream("mypic.JPG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
// Monitor print job events
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, pras);
// Wait for the print job to be done
pjDone.waitForDone();
fin.close();
}
班级PrintJobWatcher
是:
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
当您的打印作业完成后,您可以在那里进行睡眠而不是allDone
方法。