The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Traditionally, the user wants to see the page setup and print dialog boxes. From the print dialog you can select a printer, specify pages to print, and set the number of copies.
An application displays a print dialog when the user presses a button related to the print command, or chooses an item from the print menu. To display this dialog, call the printDialog
method of the PrinterJob
class:
PrinterJob pj = PrinterJob.getPrinterJob(); ... if (pj.printDialog()) { try {pj.print();} catch (PrinterException exc) { System.out.println(exc); } } ...
This method returns true
if the user clicked OK to leave the dialog, and false
otherwise. The user's choices in the dialog are constrained based on the number and format of the pages that have been set to the PrinterJob
.
The printDialog
method in the above code snippet opens a native print dialog. The PrintDialogExample.java
code example shows how to display a cross-platform print dialog.
You can change the page setup information contained in the PageFormat
object by using the page setup dialog.
To display the page setup dialog, call the pageDialog
method of the PrinterJob
class.
PrinterJob pj = PrinterJob.getPrinterJob(); PageFormat pf = pj.pageDialog(pj.defaultPage());
The page setup dialog is initialized using the parameter passed to pageDialog
. If the user clicks the OK button in the dialog, the PageFormat
instance will be created in accordance with the user�s selections, and then returned. If the user cancels the dialog, pageDialog
returns the original unchanged PageFormat.
Usually the Java 2D Printing API requires an application to display a print dialog, but in sometimes it's possible to print without showing any dialog at all. This type of printing is called silent printing. It may be useful in specific cases, such as, when you need to print a particular database weekly report. In the other cases it is always recommended to inform the user when a print process is starting.