打印的时候使用PrintJob printjob = getToolkit().getPrintJob(this, "Print Test", null);
这样会弹出一个对话框设置打印的一些选项,现在我不想让这个对话框出现,而是按照默认的设置进行打印,有没有办法啊?

解决方案 »

  1.   

    使用Toolkit.getPrintJob(Frame f, String jobtitle, Properties prop)设置prop也不行啊
      

  2.   

    如果没记错的话,print API中有一个默认的不用弹出对话框的方法,直接就可以打印
    看下它的API 吧,1.4以上的
      

  3.   

    给你个例子:
    package print;import javax.print.*; 
    import javax.print.attribute.*; 
    import java.io.*; public class Printing { 
      public static void main(String args[]) throws Exception { 
        String filename = "C:\\work\\sina_kjsd.gif"; 
        PrintRequestAttributeSet pras = 
          new HashPrintRequestAttributeSet();
       
        DocFlavor flavor = null;
         //flavor = DocFlavor.INPUT_STREAM.PNG; 
       // DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8;
        flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;//lookupDefaultPrintService   lookupPrintServices
    //    PrintService printService[] = 
    //      PrintServiceLookup.lookupPrintServices(flavor, pras);
       
        
        //锟斤拷锟揭伙拷锟斤拷锟侥拷系拇锟接★拷锟�
         
        PrintService defaultService = 
          PrintServiceLookup.lookupDefaultPrintService();
        System.out.println("获取默认打印服务�"+defaultService);//    PrintService service = ServiceUI.printDialog(null, 200, 200, 
    //      printService, defaultService, flavor, pras);
        PrintService service = defaultService;
        DocFlavor   printerFlavor[] = service.getSupportedDocFlavors();
        for(int i = 0;i<printerFlavor.length;i++){
         System.out.println("SupportedDocFlavors"+printerFlavor[i]);
        }
        
        System.out.println("当前打印机是否支持该格式"+defaultService.isDocFlavorSupported(flavor));
        if (service != null &&defaultService.isDocFlavorSupported(flavor))    { 
          DocPrintJob job = service.createPrintJob();
          System.out.println("创建打印任务 = "+job);
          FileInputStream fis = new FileInputStream(filename);
          DocAttributeSet das = new HashDocAttributeSet(); 
          //锟斤拷锟斤拷要锟斤拷印锟斤拷锟侥碉拷
    //    Doc doc = new SimpleDoc(fis, flavor, das);
          ByteArrayInputStream os = null;
          String str = "这是个测试 &#65533; ";
           char [] b = null;
    ;b = str.toCharArray();
         // os = new CharArrayInputStream(b);
    //str.getBytes("gb2312");
    os = new ByteArrayInputStream(str.getBytes("gb2312"));
          Doc doc = new SimpleDoc(os, flavor, null);
          //Doc doc = new SimpleDoc(fis, flavor, null);
          System.out.println("锟斤拷锟斤拷要锟斤拷印锟斤拷锟侥碉拷 ="+doc);     job.print(doc, pras);
         // Thread.sleep(10000); 
        } 
        System.out.println("print test----------0007 ");
        System.exit(0); 
      }