java文档中说PrinterJob类中的getPrinterJob()方法无论返回的打印机是否可用都不会返回一个null值。我查阅过一些文献有介绍说可以用getPrintService()方法测试返回的数值是否为null来判断打印机是否可用。 但是我不明白的是,在什么情况下打印机不可用?请排除打印机损坏的情况,因为之前我用过PrinterJob.getPrinterJob()==null 来判断打印机是否可用,结果出现PrinterException,然后我再从word里打印文件打印机仍然可以正常打印。
请高手们为我解析一下。 java文档参考原文: 
PrinterJob java.awt.print.PrinterJob.getPrinterJob() 

Creates and returns a PrinterJob which is initially associated with the default printer. If no printers are available on the system, a PrinterJob will still be returned from this method, but getPrintService() will return null, and calling print with this PrinterJob might generate an exception. Applications that need to determine if there are suitable printers before creating a PrinterJob should ensure that the array returned from lookupPrintServices is not empty. 

相关文档原文参考: 

One point you should keep in mind is that sometimes a printer may not be available on the machine on which your code is running.In this case the getPrintService() method will return null,so it's a good idea to call the method and test the reference that is returned,even if you don't want to obtain details of the printer. 

解决方案 »

  1.   

    这个可不可用不是交给java来判断的.你只需在代码中将打印设置界面调出来就可以了.
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPageable("");
    if (printJob.printDialog()) {
        try {
               printJob.print();
            } catch (Exception ee) {}
         }
      

  2.   

    说实话,前两天就看到这个帖子了,不会也就没进,
    这里还是帮楼主顶上来,
    学习!
    关注ing!