打印机位置未在常用打印对话框中显示

时间:2016-02-09 12:25:46

标签: java swing printing

我正在使用带有DialogTypeSelection.COMMON属性的printDialog。请参阅以下示例代码。 打印机位置不会显示在生成的打印对话框中。是否有任何方法可以在此类打印对话框中显示此选项。 注意:此选项在DialogTypeSelection.NATIVE对话框中可用。由于应用程序的某些特定要求,我无法转移到此类型。

提前致谢 common print dialog native print dialog

import java.awt.*;
import java.awt.event.*;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.DialogTypeSelection;
import javax.swing.*;
import java.awt.print.*;

public class HelloWorldPrinter implements Printable, ActionListener
{
  public int print(Graphics g, PageFormat pf, int page) throws PrinterException
  {

    if (page > 0)
    { /* We have only one page, and 'page' is zero-based */
      return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
    * translate by the X and Y values in the PageFormat to avoid clipping
    */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now we perform our rendering */
    g.drawString("Hello world!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
  }

  public void actionPerformed(ActionEvent e)
  {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(DialogTypeSelection.COMMON);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog(aset);
    if (ok)
    {
      try
      {
        job.print();
      }
      catch (PrinterException ex)
      {
        /* The job did not successfully complete */
      }
    }
  }

  public static void main(String args[])
  {

    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Hello World Printer");
    f.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });
    JButton printButton = new JButton("Print Hello World");
    printButton.addActionListener(new HelloWorldPrinter());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
  }
}

0 个答案:

没有答案