Java实现打印

None.gif Java实现打印 
None.gif 
None.gif一个非常完整的程序。请在代码下载中找。以下一共有三个代码哦。
None.gif
None.gif
import  java.awt. * ;
None.gif
import  javax.swing. * ;
None.gif
import  java.awt.print. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /** 
ExpandedBlockEnd.gif
*/

None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  PrintUtilities  implements  Printable  dot.gif {
InBlock.gif 
private Component componentToBePrinted;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public static void printComponent(Component c) dot.gif{
InBlock.gif   
new PrintUtilities(c).print();
ExpandedSubBlockEnd.gif }

InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public PrintUtilities(Component componentToBePrinted) dot.gif{
InBlock.gif   
this.componentToBePrinted = componentToBePrinted;
ExpandedSubBlockEnd.gif }

InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public void print() dot.gif{
InBlock.gif   PrinterJob printJob 
= PrinterJob.getPrinterJob();
InBlock.gif   printJob.setPrintable(
this);
InBlock.gif   
if (printJob.printDialog())
ExpandedSubBlockStart.gifContractedSubBlock.gif     
try dot.gif{
InBlock.gif       printJob.print();
ExpandedSubBlockStart.gifContractedSubBlock.gif     }
 catch(PrinterException pe) dot.gif{
InBlock.gif       System.out.println(
"Error printing: " + pe);
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public int print(Graphics g, PageFormat pageFormat, int pageIndex) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
if (pageIndex > 0dot.gif{
InBlock.gif     
return(NO_SUCH_PAGE);
ExpandedSubBlockStart.gifContractedSubBlock.gif   }
 else dot.gif{
InBlock.gif     Graphics2D g2d 
= (Graphics2D)g;
InBlock.gif     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
InBlock.gif     disableDoubleBuffering(componentToBePrinted);
InBlock.gif     componentToBePrinted.paint(g2d);
InBlock.gif     enableDoubleBuffering(componentToBePrinted);
InBlock.gif     
return(PAGE_EXISTS);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public static void disableDoubleBuffering(Component c) dot.gif{
InBlock.gif   RepaintManager currentManager 
= RepaintManager.currentManager(c);
InBlock.gif   currentManager.setDoubleBufferingEnabled(
false);
ExpandedSubBlockEnd.gif }

InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public static void enableDoubleBuffering(Component c) dot.gif{
InBlock.gif   RepaintManager currentManager 
= RepaintManager.currentManager(c);
InBlock.gif   currentManager.setDoubleBufferingEnabled(
true);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
import  java.awt. * ;
None.gif
import  javax.swing. * ;
None.gif
import  java.awt.event. * ;
None.gif
import  java.awt.print. * ;
None.gif
None.gif
public   class  PrintExample  extends  JFrame
ExpandedBlockStart.gifContractedBlock.gif                         
implements  ActionListener  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public static void main(String[] args) dot.gif{
InBlock.gif   
new PrintExample();
ExpandedSubBlockEnd.gif }

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public void actionPerformed(ActionEvent event) dot.gif{
InBlock.gif   PrintUtilities.printComponent(
this);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
import  java.awt. * ;
None.gif
import  javax.swing. * ;
None.gif
import  java.awt.geom. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  DrawingPanel  extends  JPanel  dot.gif {
InBlock.gif 
private int fontSize = 90;
InBlock.gif 
private String message = "Java 2D";
InBlock.gif 
private int messageWidth;
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public DrawingPanel() dot.gif{
InBlock.gif   setBackground(Color.white);
InBlock.gif   Font font 
= new Font("Serif", Font.PLAIN, fontSize);
InBlock.gif   setFont(font);
InBlock.gif   FontMetrics metrics 
= getFontMetrics(font);
InBlock.gif   messageWidth 
= metrics.stringWidth(message);
InBlock.gif   
int width = messageWidth*5/3;
InBlock.gif   
int height = fontSize*3;
InBlock.gif   setPreferredSize(
new Dimension(width, height));
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public void paintComponent(Graphics g) dot.gif{
InBlock.gif   
super.paintComponent(g);
InBlock.gif   Graphics2D g2d 
= (Graphics2D)g;
InBlock.gif   
int x = messageWidth/10;
InBlock.gif   
int y = fontSize*5/2;
InBlock.gif   g2d.translate(x, y);
InBlock.gif   g2d.setPaint(Color.lightGray);
InBlock.gif   AffineTransform origTransform 
= g2d.getTransform();
InBlock.gif   g2d.shear(
-0.950);
InBlock.gif   g2d.scale(
13);
InBlock.gif   g2d.drawString(message, 
00);
InBlock.gif   g2d.setTransform(origTransform);
InBlock.gif   g2d.setPaint(Color.black);
InBlock.gif   g2d.drawString(message, 
00);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}
 
None.gif
None.gif

转载于:https://www.cnblogs.com/ctfzh/archive/2007/07/09/811360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值