Java by API/java.awt.print/Printable

Материал из Java эксперт
Перейти к: навигация, поиск

implements Printable

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] args) {
    try {
      PrinterJob pjob = PrinterJob.getPrinterJob();
      pjob.setJobName("Graphics Demo Printout");
      pjob.setCopies(1);
      pjob.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
          if (pageNum > 0) // we only print one page
            return Printable.NO_SUCH_PAGE; // ie., end of job
          pg.drawString("www.jexp.ru", 10, 10);
          return Printable.PAGE_EXISTS;
        }
      });
      if (pjob.printDialog() == false) // choose printer
        return; 
      pjob.print(); 
    } catch (PrinterException pe) {
      pe.printStackTrace();
    }
  }
}





Printable.NO_SUCH_PAGE

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] args) {
    try {
      PrinterJob pjob = PrinterJob.getPrinterJob();
      pjob.setJobName("Graphics Demo Printout");
      pjob.setCopies(1);
      pjob.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
          if (pageNum > 0) // we only print one page
            return Printable.NO_SUCH_PAGE; // ie., end of job
          pg.drawString("www.jexp.ru", 10, 10);
          return Printable.PAGE_EXISTS;
        }
      });
      if (pjob.printDialog() == false) // choose printer
        return; 
      pjob.print(); 
    } catch (PrinterException pe) {
      pe.printStackTrace();
    }
  }
}





Printable.PAGE_EXISTS

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] args) {
    try {
      PrinterJob pjob = PrinterJob.getPrinterJob();
      pjob.setJobName("Graphics Demo Printout");
      pjob.setCopies(1);
      pjob.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
          if (pageNum > 0) // we only print one page
            return Printable.NO_SUCH_PAGE; // ie., end of job
          pg.drawString("www.jexp.ru", 10, 10);
          return Printable.PAGE_EXISTS;
        }
      });
      if (pjob.printDialog() == false) // choose printer
        return; 
      pjob.print(); 
    } catch (PrinterException pe) {
      pe.printStackTrace();
    }
  }
}