📄 print.html
字号:
g2.setColor(Color.black);
paint(g2);
return Printable.PAGE_EXISTS;
}
</PRE><FONT FACE="Verdana, Arial, Helvetica">
<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG>
The printing Graphics2D is based on the <code>BufferedImage</code> class and
on some platforms does not default to a foreground color of black. If
this is the case on your platform, you have to add
<code>g2.setColor(Color.black)</code> to the <code>print</code> method
before the <code>paint</code> invocation.
</BLOCKQUOTE>
<H3>Printing a Swing Component</H3>
<IMG SRC="./Art/swbutton.gif" ALIGN=LEFT>
Printing a Swing component is almost the same as printing an AWT
component, except the <code>MyButton</code> subclass does not
need a <code>paint</code> method implementation. It does, however, have
a <code>print</code> method that calls the <CODE>paint</CODE>
method for the component. The <code>paint</code> method implementation
is not needed because Swing components know how to paint themselves.
<P>
Here
is the complete <A HREF="./Code/Swing/printbutton.java">printbutton.java</A>
source code for Swing.
</FONT><PRE>
class MyButton extends JButton implements Printable {
public MyButton() {
super("MyButton");
}
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
Font f = new Font("Monospaced", Font.PLAIN,12);
g2.setFont (f);
paint(g2);
return Printable.PAGE_EXISTS;
}
</PRE><FONT FACE="Verdana, Arial, Helvetica">
If you extend a <code>JPanel</code> and implement <code>Printable</code>,
you can print a panel component and all of its contents.
</FONT><PRE>
public class printpanel extends JPanel
implements ActionListener, Printable {
</PRE><FONT FACE="Verdana, Arial, Helvetica">
Here is
the <A HREF="./Code/Swing/printpanel.java">printpanel.java</A> code
that prints a <code>JPanel</code> object and the <code>JButton</code>
it contains, and the
<A HREF="./Code/Swing/ComponentPrinterFrame.java">ComponentPrinterFrame.java</A>
code that prints a <code>JFrame</code> object and the
<code>JButton</code>, <code>JList</code>, <code>JCheckBox</code>,
and <code>JComboBox</code> components it contains.
<H3>Printing Graphics in Swing</H3>
In the same way the AWT example subclassed a <code>Button</code>
component and implemented the <code>paint</code> method to
draw the button, you can subclass an AWT or Swing component and implement
the <code>paint</code> method to render 2D graphics to the screen or printer.
The <A HREF="./Code/Swing/ShapesPrint.java">ShapesPrint.java"</A>
Swing application borrowed from
<A HREF="http://java.sun.com/docs/books/tutorial/index.html">The Java Tutorial</A>
shows how this is done. It is modified for this article to include
a <code>TextLayout</code> object.
<P>
<IMG SRC="./Art/shapes.gif">
<P>
The <code>paintComponent</code> method calls the <code>drawShapes</code>
method to render the 2D graphics to the screen when the application starts.
When you click the <code>Print</code> button, a printer graphics context
is created and passed to the <code>drawShapes</code> method for printing.
<H3>Print Dialog</H3>
It is easy to display a Print dialog
so the end user can interactively change the print job
properties. The <code>actionPerformed</code> method of
the previous Swing example is modified here
to do just that.
<P>
<IMG SRC="./Art/dialog.gif">
<P>
</FONT><PRE>
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable((MyButton) e.getSource());
if(printJob.printDialog()){
try { printJob.print(); }
catch (Exception PrinterExeption) { }
}
}
</PRE><FONT FACE="Verdana, Arial, Helvetica">
<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG>In Swing, the
<code>printJob.setPageable((MyButton) e.getSource());</code>
statement can be written as
<code>printJob.setPrintable((MyButton) e.getSource());</code>. The
difference is <code>setPrintable</code> is for applications that
do not know the number of pages they are printing.
If you use <code>setPrintable</code>, you need to add
<code>if(pi >= 1){ return Printable.NO_SUCH_PAGE: }</code>
to the beginnig of the <code>print</code> method.
</BLOCKQUOTE>
<H3>Page Setup Dialog</H3>
You can add a line of code that tells the <code>PrinterJob</code> object
to display a Page dialog so the end user can interactively modify the
page format for printing in portrait, landscape, or reverse landscape mode.
The <code>actionPerformed</code> method of the previous Swing example is
modified here to display Page and Print dialogs.
<P>
<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG> Some platforms
do not support a page dialog. On those platforms, the <code>pageDialog</code>
call simply returns the passed-in <code>PageFormat</code> object
and no dialog appears.
</BLOCKQUOTE>
</FONT><PRE>
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable((MyButton) e.getSource());
PageFormat pf = printJob.pageDialog(printJob.defaultPage());
if(printJob.printDialog()){
try { printJob.print(); } catch (Exception ex) { }
}
}
</PRE><FONT FACE="Verdana, Arial, Helvetica">
<H3>Printing a Collection of Pages</H3>
You can use the <code>Book</code> class to print a collection
of pages that you append to the book. The pages can be in any order
and have different page formats.
<P>
<IMG SRC="./Art/print2.gif" ALIGN=LEFT>
The <A HREF="./Code/Swing/print2button.java">print2button.java</A>
example puts the <code>Print</code> and <code>Print 2</code> buttons of
type <code>MyButton</code> on a panel.
It creates a book that contains the pages to print.
When you click either button, the book prints one copy of the
<code>Print</code> button in
landscape mode and two copies of the <code>Print 2</code> button
in portrait more, as specified in the <code>actionPerformed</code>
method implementation shown here.
<BLOCKQUOTE>
<HR>
<STRONG>Note:</STRONG>
Currently a bug restricts the Solaris platform to
only print in portrait mode.
<HR>
</BLOCKQUOTE>
</FONT><PRE>
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
/* Set up Book */
PageFormat landscape = printJob.defaultPage();
PageFormat portrait = printJob.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
portrait.setOrientation(PageFormat.PORTRAIT);
Book bk = new Book();
bk.append((Printable)b, landscape);
bk.append((Printable)b2, portrait, 2);
printJob.setPageable(bk);
try { printJob.print(); } catch (Exception ex) { }
}
</PRE><FONT FACE="Verdana, Arial, Helvetica">
<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>
</FONT>
</TD>
</TR>
</TABLE>
<TABLE WIDTH=600><!-- Copyright Insert -->
</TABLE>
<BR CLEAR="ALL">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">
<TR>
<TD VALIGN="TOP">
<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<P ALIGN=CENTER>
<FONT SIZE="-1" COLOR="#999999">[ This page was updated: <!--#echo var="LAST_MODIFIED" --> 16-Aug-99 ]</font></P>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<SPACER TYPE="BLOCK" HEIGHT="1" WIDTH="1"></TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2">
<A HREF="http://java.sun.com/products/">Products & APIs</A> |
<A HREF="/developer/index.html">Developer Connection</A> |
<A HREF="/developer/infodocs/index.shtml">Docs & Training</A> |
<A HREF="/developer/support/index.html">Online Support</A><BR>
<A HREF="/developer/community/index.html">Community Discussion</A> |
<A HREF="http://java.sun.com/industry/">Industry News</A> |
<A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |
<A HREF="http://java.sun.com/casestudies">Case Studies</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<SPACER TYPE="BLOCK" HEIGHT="1" WIDTH="1"></TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2">
<a href="/developer/faq/index.html">FAQ</a> |
<a href="/feedback/index.html">Feedback</a> |
<a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?
db=javasoft&dest=http://java.sun.com/">Map</a> |
<A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
</FONT>
</CENTER>
<!-- <CENTER>
<INPUT TYPE="text" NAME="search" VALUE="" size="23">
<INPUT TYPE="submit" NAME="Search" VALUE="Search">
</CENTER>
-->
</TD>
</TR>
<TR>
<TD>
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
<TR>
<TD WIDTH="50%">
<FONT SIZE="-2">
For more information on Java technology<BR>
and other software from Sun Microsystems, call:<BR>
</FONT>
<FONT SIZE="-1">
(800) 786-7638<BR></FONT>
<FONT SIZE="-2">
Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR>
</FONT>
</TD>
<TD ALIGN="RIGHT" WIDTH="50%">
<A HREF="http://www.sun.com">
<IMG SRC="/images/lgsun.gif" width="64"
height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
<FONT SIZE="-2">
Copyright © 1995-99
<A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
All Rights Reserved. <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>.
</FONT>
</TD></TR></TABLE>
</FORM>
</TD>
</TR>
</TABLE>
<!-- End Copyright Insert -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -