 
                    
                
         
    
    
    
	
		
			
			
				
  
  
  
  
  
 
			
			
				
	
		
		
		
			
 Java实现打印
Java实现打印 
 
 
 一个非常完整的程序。请在代码下载中找。以下一共有三个代码哦。
一个非常完整的程序。请在代码下载中找。以下一共有三个代码哦。

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


 /** *//**
/** *//** 
 */
*/


 public class PrintUtilities implements Printable
public class PrintUtilities implements Printable  {
{
 private Component componentToBePrinted;
 private Component componentToBePrinted;


 public static void printComponent(Component c)
 public static void printComponent(Component c)  {
{
 new PrintUtilities(c).print();
   new PrintUtilities(c).print();
 }
 }
 
 

 public PrintUtilities(Component componentToBePrinted)
 public PrintUtilities(Component componentToBePrinted)  {
{
 this.componentToBePrinted = componentToBePrinted;
   this.componentToBePrinted = componentToBePrinted;
 }
 }
 
 

 public void print()
 public void print()  {
{
 PrinterJob printJob = PrinterJob.getPrinterJob();
   PrinterJob printJob = PrinterJob.getPrinterJob();
 printJob.setPrintable(this);
   printJob.setPrintable(this);
 if (printJob.printDialog())
   if (printJob.printDialog())

 try
     try  {
{
 printJob.print();
       printJob.print();

 } catch(PrinterException pe)
     } catch(PrinterException pe)  {
{
 System.out.println("Error printing: " + pe);
       System.out.println("Error printing: " + pe);
 }
     }
 }
 }


 public int print(Graphics g, PageFormat pageFormat, int pageIndex)
 public int print(Graphics g, PageFormat pageFormat, int pageIndex)  {
{

 if (pageIndex > 0)
   if (pageIndex > 0)  {
{
 return(NO_SUCH_PAGE);
     return(NO_SUCH_PAGE);

 } else
   } else  {
{
 Graphics2D g2d = (Graphics2D)g;
     Graphics2D g2d = (Graphics2D)g;
 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
 disableDoubleBuffering(componentToBePrinted);
     disableDoubleBuffering(componentToBePrinted);
 componentToBePrinted.paint(g2d);
     componentToBePrinted.paint(g2d);
 enableDoubleBuffering(componentToBePrinted);
     enableDoubleBuffering(componentToBePrinted);
 return(PAGE_EXISTS);
     return(PAGE_EXISTS);
 }
   }
 }
 }


 public static void disableDoubleBuffering(Component c)
 public static void disableDoubleBuffering(Component c)  {
{
 RepaintManager currentManager = RepaintManager.currentManager(c);
   RepaintManager currentManager = RepaintManager.currentManager(c);
 currentManager.setDoubleBufferingEnabled(false);
   currentManager.setDoubleBufferingEnabled(false);
 }
 }
 
 

 public static void enableDoubleBuffering(Component c)
 public static void enableDoubleBuffering(Component c)  {
{
 RepaintManager currentManager = RepaintManager.currentManager(c);
   RepaintManager currentManager = RepaintManager.currentManager(c);
 currentManager.setDoubleBufferingEnabled(true);
   currentManager.setDoubleBufferingEnabled(true);
 }
 }
 }
}


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

 public class PrintExample extends JFrame
public class PrintExample extends JFrame

 implements ActionListener
                         implements ActionListener  {
{

 public static void main(String[] args)
 public static void main(String[] args)  {
{
 new PrintExample();
   new PrintExample();
 }
 }


 public PrintExample()
 public PrintExample()  {
{
 super("Printing Swing Components");
   super("Printing Swing Components");
 WindowUtilities.setNativeLookAndFeel();
   WindowUtilities.setNativeLookAndFeel();
 addWindowListener(new ExitListener());
   addWindowListener(new ExitListener());
 Container content = getContentPane();
   Container content = getContentPane();
 JButton printButton = new JButton("Print");
   JButton printButton = new JButton("Print");
 printButton.addActionListener(this);
   printButton.addActionListener(this);
 JPanel buttonPanel = new JPanel();
   JPanel buttonPanel = new JPanel();
 buttonPanel.setBackground(Color.white);
   buttonPanel.setBackground(Color.white);
 buttonPanel.add(printButton);
   buttonPanel.add(printButton);
 content.add(buttonPanel, BorderLayout.SOUTH);
   content.add(buttonPanel, BorderLayout.SOUTH);
 DrawingPanel drawingPanel = new DrawingPanel();
   DrawingPanel drawingPanel = new DrawingPanel();
 content.add(drawingPanel, BorderLayout.CENTER);
   content.add(drawingPanel, BorderLayout.CENTER);
 pack();
   pack();
 setVisible(true);
   setVisible(true);
 }
 }


 public void actionPerformed(ActionEvent event)
 public void actionPerformed(ActionEvent event)  {
{
 PrintUtilities.printComponent(this);
   PrintUtilities.printComponent(this);
 }
 }
 }
}


 import java.awt.*;
import java.awt.*;
 import javax.swing.*;
import javax.swing.*;
 import java.awt.geom.*;
import java.awt.geom.*;


 public class DrawingPanel extends JPanel
public class DrawingPanel extends JPanel  {
{
 private int fontSize = 90;
 private int fontSize = 90;
 private String message = "Java 2D";
 private String message = "Java 2D";
 private int messageWidth;
 private int messageWidth;
 
 

 public DrawingPanel()
 public DrawingPanel()  {
{
 setBackground(Color.white);
   setBackground(Color.white);
 Font font = new Font("Serif", Font.PLAIN, fontSize);
   Font font = new Font("Serif", Font.PLAIN, fontSize);
 setFont(font);
   setFont(font);
 FontMetrics metrics = getFontMetrics(font);
   FontMetrics metrics = getFontMetrics(font);
 messageWidth = metrics.stringWidth(message);
   messageWidth = metrics.stringWidth(message);
 int width = messageWidth*5/3;
   int width = messageWidth*5/3;
 int height = fontSize*3;
   int height = fontSize*3;
 setPreferredSize(new Dimension(width, height));
   setPreferredSize(new Dimension(width, height));
 }
 }


 public void paintComponent(Graphics g)
 public void paintComponent(Graphics g)  {
{
 super.paintComponent(g);
   super.paintComponent(g);
 Graphics2D g2d = (Graphics2D)g;
   Graphics2D g2d = (Graphics2D)g;
 int x = messageWidth/10;
   int x = messageWidth/10;
 int y = fontSize*5/2;
   int y = fontSize*5/2;
 g2d.translate(x, y);
   g2d.translate(x, y);
 g2d.setPaint(Color.lightGray);
   g2d.setPaint(Color.lightGray);
 AffineTransform origTransform = g2d.getTransform();
   AffineTransform origTransform = g2d.getTransform();
 g2d.shear(-0.95, 0);
   g2d.shear(-0.95, 0);
 g2d.scale(1, 3);
   g2d.scale(1, 3);
 g2d.drawString(message, 0, 0);
   g2d.drawString(message, 0, 0);
 g2d.setTransform(origTransform);
   g2d.setTransform(origTransform);
 g2d.setPaint(Color.black);
   g2d.setPaint(Color.black);
 g2d.drawString(message, 0, 0);
   g2d.drawString(message, 0, 0);
 }
 }
 }
} 


 
		 
		posted @ 
2007-07-09 16:12 
紫色幽灵 
阅读(
1941) 
评论() 
 
收藏 
举报