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发行说明。
Another common printing task is to print the contents of a window or a frame, either in whole, or in part. The window may contain the following components: toolbars, buttons sliders, text labels, scrollable text areas, images, and other graphical content. All of these components are printed using the following methods of the Java 2D printing API:
java.awt.Component.print(Graphics g); java.awt.Component.printAll(Graphics g);
The following figure represents a simple user interface.
The code to create this UI is located in the sample program PrintUIWindow.java
.
To print this window, modify the code in the earlier examples which printed text or images. The resulting code should appear as follows:
public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); // Print the entire visible contents of a // java.awt.Frame. frame.printAll(g); return PAGE_EXISTS; }
printAll
method is the only difference between this example and examples to print text or image. The print(Graphics g)
method mirrors the java.awt.Component.paint(Graphics g)
method used for on-screen rendering. Use the print()
method rather than the paint()
method as the Components
class may have overridden the print()
method to handle the printing case differently. The printAll(Graphics g)
method prints the component and all its subcomponents. This method is usually used to print object such as a complete window, rather than a single component.