????DeveloperWork????????????Java????????????? ??  Oracle??????Printing??Guide ?? ???????????? java.awt.print and javax.print
????The basic printing operations are represented in the following sections:
????A Basic Printing Program – this section describes the Printable interface and presents a basic printing program.
????Using Print Setup Dialogs– this sections explains how to display the Print Setup Dialog.
????Printing a Multiple Page Document – this section explains how to use pagination for printing a multiple page document.
????Working with Print Services and Attributes ndash; this section teaches you about print services?? how to specify the print data format?? and how to create print job using the javax.print package.
????Printing the Contents of a User Interface – this section explains how to print the contents of a window or a frame.
????Printing Support in Swing Components - this section provides a brief description of the related printing functionality inSwing and refers to specific Swing classes and interfaces.
????Java??????????????????????ο??????HelloWOrldPrinter????????????????PrinterJob??Printable?????????ɡ?
import java.awt.*;
import java.awt.event.*;
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) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
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);
}
}