Print the interface of the Java application. You can specify any of the components in this interface.

xiaoxiao2021-03-06  21

Today, the team leader said to print the interface of the FSI system. I called me. I was first. I watched the article under the article under Google. I wrote the following tool class.

/ * * Created on 2005-3-23 * / package com.dengyin.awt.printUtils;

Import java.awt.component; import java.awt.frame; import java.awt.graphchics; import java.awt.printjob; import java.awt.toolkit; import java.util.properties;

/ ** * PrintUTIL is used to print a Java application (FRAME) or a component interface in this application. * It does not apply to print text, its role is equivalent to intercepting a Java application (Frame) or an image of a group * in this application, then print it out with a printer. * * I am mainly refer to this article when I write this class * * @see Java print program design * * @see java.awt.printjob * @version 0.1 2005 -3-23 * @Author Deng 胤 * / public class printutil {

/ ** * * @Param Frame * Java application master Frame * @Param com * You want to print the components in the main frame (can be any type of inheritance java.awt.com) * / public static void Print Frame Frame, Component COM) {

Toolkit Kit = Toolkit.getDefaultToolkit (); // Get Toolbox Properties PROPS = New Properties (); Props.Printer ("AWT.PRINT.PRINTER", "DURANGO"); // Setting Print Properties Props.Put ("AWT .print.numcopies "," 2 "); if (kit! = null) {// Get the print object comes with the toolbox PrintJob PrintJob = Kit.GetPrintJob (Frame," Print Frame ", Props); if (PrintJob! = NULL) {graphics pg = printJob.getgraphs (); // Get the graphical environment IF (PG! = null) {Try {com.printall (pg); // Print the form and all of its components} Finally {pg.dispose (); // Logout graphic environment}} printjob.end (); // End print job}}

}

/ ** * This method relies on PrintPreviewDialog * * @Param Frame * Java application's main frame * @Param COM * You want to preview the printing of the components in the main frame (can be any type of inheritance java.awt.component) * / public static void printPreview (Frame frame, Component com) {PrintPreviewDialog pd = new PrintPreviewDialog (frame, "print preview", true, com); pd.setSize (400, 600); pd.setVisible (true);}}

Package com.dengyin.awt.printUtils;

Import java.awt.event. *; import java.awt. *; import java.awt.print. *; import javax.swing. *;

Import com.szallcom.tools.systemproperties;

Import java.awt.geom. *;

/ ** * I just take this example of the Internet to change the details, please see * * @see Java print program design * * @Author Deng * / public class printpreviewDialog Extends JDialog Implements ActionListener {

Private jbutton closebutton = new jbutton ("close");

Private jbutton printbutton = new jbutton ("OK! I Want to Print Now.");

Private JPanel ButtonPanel = New JPANEL ();

Private previewcanvas canvas;

PRIVATE FRAME PARENT;

Private component COM;

Public PrintPreviewDialog (Frame Parent, String Title, Boolean Modal, Component Pt) {Super (PARENT, TITLE, MODAL); this.Parent = Parent; this.com = pt; canvas = new previewcanvas (pt); setLayout ();

Private void setLayout () {this.getContentPane (). setLayout (new borderLayout ()); this.getContentPane (). add (canvas, borderlayout.center);

CloseButton.SETMNEMONIC ('n'); CloseButton.AddActionListener (this);

printButton.setMnemonic ( 'N'); printButton.addActionListener (this); buttonPanel.add (closeButton); buttonPanel.add (printButton); this.getContentPane () add (buttonPanel, BorderLayout.SOUTH);. this.setBounds (( Int ((SystemProperties.Screen_Width - 400) / 2), (INT) ((SystemProperties.Screen_HEIGHT - 400) / 2), 400, 400);

Public void actionperformed (ActionEvent EVT) {Object Src = evt.getsource (); if (src == closebutton) CloseAction (); else if (src == printbutton) printnow ();

Private void printnow () {PrintUTIL.Print (PARENT, COM);

Private vid closection () {this.setvisible (false); this.dispose ();}

Class PreviewCanvas Extends JPanel {Private Int CurrentPage = 0;

Private component preview;

Public PreviewCanvas (Component Pt) {preview = pt;}

Public void PaintComponent (GRAPHICS G) {Super.PaintComponent (G); Graphics2D G2 = (Graphics2D) G; PageFormat Pf = PrinterJob.getPrinterJob (). DefaultPage ();

Double Xoff; double scale; double px = pf.getWidth (); double py = pf.getHEight (); double sx = getWidth () - 1; double syliş son

Preview.Print (G2);

}

For details, please refer to http://www-900.ibm.com/developerworks/cn/java/l-javaprint/index.shtml

The above implementation When the width of the form is greater than the width of the paper. You can only print a part.

After your own east, we solved this problem.

/ * * Created on 2005-3-23 * / package com.fsillc.print.test;

Import java.awt.dimponent; import java.awt.frame; import java.awt.graphics; import java.awt.graphics2d; import java.awt.printjob;

import java.awt.Toolkit; import java.awt.geom.AffineTransform; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print. PrinterJob; import java.util.properties;

/ ** * PrintUTIL is used to print a Java application (FRAME) or a component interface in this application. * It does not apply to print text, its role is equivalent to intercepting a Java application (Frame) or an image of a group * in this application, then print it out with a printer. * * I am mainly refer to this article when I write this class * * @see Java print program design * * @see java.awt.printjob * @version 0.1 2005 -3-23 * @Author Deng * / Public Abstract Class Printil Implements Printable {/ ** * * @Param Frame * Java application main frame * @Param com * You want to print components in the main frame (you can Yes, any inheritance java.awt.component type) * / public static void print (Component COM) {/ * * Toolkit Kit = Toolkit.getDefaultToolkit (); // Get Toolbox Properties PROPS = * New Properties (); Props. PUT ("AWT.PRINT.PRINTER", "DURANGO"); // Settings Print Properties * Props.Put ("AWT.PRINT.NUMCOPIES", "2"); if (kit! = null) {* // Get Print Object PrintJob PrintJob = Kit.getPrintJob (Frame, "Print * Frame", Props; if (PrintJob! = NULL) {Graphics Pg = * PrintJob. GETGRAPHICS (); // Get the graphic environment of the print object * * if (pg! = null) {try {com.printall (pg); // Print the form and all of its components} finally {* pg.dispose ); // logout graphic environment}} printJob.end (); // End print job}} * / printerJob Job = printerJob.getPrinterJob (); Job.SetJobName ("Maxim 1"); job.setprintable ((Printable) COM); try {job.print ();} catch (printerexception e) {// Todo auto-generated catch block e.printstacktrace ();}}

/ ** * This method relies on PrintPreviewDialog * * @Param Frame * Java application's main frame * @Param COM * You want to preview the printing of the components in the main frame (can be any type of inheritance java.awt.component) * / public static void printPreview (Frame p, Component com) {PrintPreviewDialog pd = new PrintPreviewDialog (p, "print Preview", true, com); pd.setSize (800, 600); pd.setVisible (true);} public static int printAble (Component c, Graphics g, PageFormat pageFormat, int pageIndex) {if (pageIndex == 0) {// this.setSize ((int) pageFormat.getImageableWidth (), (int) pageFormat // .getImageableHeight () ); // this.validate (); // g.Translate ((int) PageFormat.getiMageAblex (), (int) PageformAT // .GetImageable ()); // this.printall (g); // Return Page_Exists ;

Dimension prefSize = c.getPreferredSize (); c.setSize (prefSize); c.validate (); double ratio = pageFormat.getImageableWidth () / prefSize.width; Graphics2D g2d = (Graphics2D) g; // translate origin toimageable origin g2d .transform (AffineTransform.getTranslateInstance (pageFormat .getImageableX (), pageFormat.getImageableY ())); // // scale output g2d.transform (AffineTransform.getScaleInstance (ratio, ratio)); c.printAll (g2d); return PAGE_EXISTS } Return no_such_page;

}

Package com.fsillc.print.test;

Import java.awt.event. *; import java.awt. *; import java.awt.print. *; import javax.swing. *;

Import java.awt.geom. *;

Public Class PrintPreviewDialog Extends JDialogImplements ActionListener {

private JButton closeButton = new JButton ( "Close"); private JButton printButton = new JButton ( "Ok I want to print the current tab!."); private JPanel buttonPanel = new JPanel (); private PreviewCanvas canvas; private Component c; Public Print, String Title, Boolean Modal, Component Pt {Super (PARENT, TITLE, MODAL); THIS.C = Pt; Canvas = New PreviewCanvas (Pt); setLayout ();}

Private void setLayout () {this.getContentPane (). setLayout (new borderLayout ()); this.getContentPane (). add (canvas, borderlayout.center);

closeButton.setMnemonic ( 'N'); closeButton.addActionListener (this); printButton.setMnemonic ( 'N'); printButton.addActionListener (this); buttonPanel.add (closeButton); buttonPanel.add (printButton); this.getContentPane ( ) .add (ButtonPanel, BorderLayout.South); this.SetBounds ((forkit.getDefault (). getScreensize (). Width - 400) / 2), (int) ((Toolkit.getDefaultToolkit (). getScreensize (). HEIGHT - 400) / 2), 400, 400);

Public void actionPerformed (ActionEvent EVT) {Object Src = evt.getsource (); if (src == closebutton) CloseAction (); Else IF (src == printbutton) Printil.Print (c);}

Private vid closection () {this.setvisible (false); this.dispose ();}

Class PreviewCanvas Extends JPanel {Private Int CurrentPage = 0; Private Component Preview;

Public previewcanvas (Component Pt) {

Preview = pt;} public void PaintComponent (graphics g) {super.paintcomponent (g); graphics2d g2 = (graphics2d) g; pageformat pf = printerJob.getPrinterJob (). DefaultPage ();

Double Xoff; double scale; double px = pf.getWidth (); double py = pf.getHEight (); double sx = getWidth () - 1; double syliş son

Rectangle2D Page = New Rectangle2D.double (0, 0, PX, PY); G2.SetPaint (Color.White); g2.fill (page); g2.setpaint (color.black); g2.draw (page);

Try {((Printable) .print (G2, PF, CurrentPage);} catch (printerexception pe);} catch (printerexception PE) {g2.draw (new line2d.double (0, 0, px, py)); g2.draw (new line2d .Double (0, px, 0, py));}}

}

/ * * Created on 2005-3-23 * / package com.fsillc.print.test;

import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt .event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import javax.swing.JButton; import javax .swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield;

/ ** * @author Deng * / public class printTest Extends Jframe Implements Printable {

Private JTextField TXT1;

Private jlabel lab1;

Private jbutton button;

Private jbutton button1;

Private void printframe () {PrintUTIL.Print (this);

private void previewFrame () {PrintUtil.printPreview (this, this);} / ** * @param title * @throws java.awt.HeadlessException * / public PrintTest (String title) throws HeadlessException {super (title); this.addWindowListener (New WindowsListener ()); txt1 = new jtextfield ("Dafadsfads"); lab1 = new jlabel ("dsafad"); button = new jbutton ("Print!"); button1 = new jbutton ("preview"); Container C = this.getContentPane (); C.SetLayout (New flowLayout ()); C.Add (txt1); C.Add (lab1); c.Add (button); c.Add (Button1); button.addActionListener (New ActionListener () {

Public void actionperformed (ActionEvent E) {printframe ();}}); button1.addactionListener (new activityListener () {

public void actionPerformed (ActionEvent e) {previewFrame ();}});} public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {return PrintUtil.printAble (this, graphics, pageFormat, pageIndex);}

Public static void main (string [] args) {jframe f = new PrintTest ("Print Me!"); F.setSize (600, 400); f.setVisible (TRUE);}

Private class windowslistener extends windowadapter {

Public void windowclosing (windowevent e) {system.exit (0);}}

}

Run this TEST effect.

转载请注明原文地址:https://www.9cbs.com/read-43657.html

New Post(0)