您好,欢迎访问三七文档
猪哥解答:我这里有以前收藏的代码,两个类实现了简易的文本打印机的功能,包括预览。简单跟你说一下。1、PrinterDemo.java主体类,也是入口类,里面有main方法可以直接在Eclipse中调试运行,他实现了从本地磁盘读取文本类文件打印以及打印预览的功能,其中File动作按钮中的PrintPreviw就是打印预览功能,你可以运行看看。2、PrintPreview.java打印预览类,这是专门为预览打印设计的类,通过他的构造方法可以构造出一个预览类,PrinterDemo中的预览功能就是调用了这个类。两个类的源码:【PrinterDemo.java】的源码:importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.Event;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.awt.print.PageFormat;importjava.awt.print.Printable;importjava.awt.print.PrinterException;importjava.awt.print.PrinterJob;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.util.Vector;importjavax.swing.AbstractAction;importjavax.swing.JComponent;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JScrollPane;importjavax.swing.KeyStroke;publicclassPrinterDemoextendsJFrame{privatestaticfinallongserialVersionUID=1L;privatePageFormatpageFormat;privateFilePageRendererpageRenderer;privateStringtitle;publicPrinterDemo(){super();init();PrinterJobpj=PrinterJob.getPrinterJob();pageFormat=pj.defaultPage();setVisible(true);}protectedvoidinit(){setSize(350,300);center();Containercontent=getContentPane();content.setLayout(newBorderLayout());JMenuBarmb=newJMenuBar();JMenufile=newJMenu(File,true);file.add(newFileOpenAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));file.add(newFilePrintPreviwAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.ALT_MASK));file.add(newFilePrintAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK));file.add(newFilePageSetupAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK|Event.SHIFT_MASK));file.addSeparator();file.add(newFileQuitAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));mb.add(file);JMenupage=newJMenu(Page,true);page.add(newPageNextPageAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0));page.add(newPagePreviousPageAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0));mb.add(page);setJMenuBar(mb);getContentPane().setLayout(newBorderLayout());addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}protectedvoidcenter(){DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();DimensionframeSize=getSize();intx=(screenSize.width-frameSize.width)/2;inty=(screenSize.height-frameSize.height)/2;setLocation(x,y);}publicvoidshowTitle(){intcurrentPage=pageRenderer.getCurrentPage()+1;intnumPages=pageRenderer.getNumPages();setTitle(title+-page+currentPage+of+numPages);}publicclassFileOpenActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicFileOpenAction(){super(Open...);}publicvoidactionPerformed(ActionEventae){//Popupafiledialog.JFileChooserfc=newJFileChooser(.);intresult=fc.showOpenDialog(PrinterDemo.this);if(result!=0){return;}java.io.Filef=fc.getSelectedFile();if(f==null){return;}//Loadthespecifiedfile.try{pageRenderer=newFilePageRenderer(f,pageFormat);title=[+f.getName()+];showTitle();JScrollPanejsp=newJScrollPane(pageRenderer);getContentPane().removeAll();getContentPane().add(jsp,BorderLayout.CENTER);validate();}catch(java.io.IOExceptionioe){System.out.println(ioe);}}}publicstaticvoidmain(String[]args){newPrinterDemo();}publicclassFilePrintPreviwActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicFilePrintPreviwAction(){super(PrintPreviw);}publicvoidactionPerformed(ActionEventae){newPrintPreview(pageRenderer,PrintPreviw,pageFormat.getOrientation());}}publicclassFilePrintActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicFilePrintAction(){super(Print);}publicvoidactionPerformed(ActionEventae){PrinterJobpj=PrinterJob.getPrinterJob();pj.setPrintable(pageRenderer,pageFormat);if(pj.printDialog()){try{pj.print();}catch(PrinterExceptione){System.out.println(e);}}}}publicclassFilePageSetupActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicFilePageSetupAction(){super(Pagesetup...);}publicvoidactionPerformed(ActionEventae){PrinterJobpj=PrinterJob.getPrinterJob();pageFormat=pj.pageDialog(pageFormat);if(pageRenderer!=null){pageRenderer.pageInit(pageFormat);showTitle();}}}publicclassFileQuitActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicFileQuitAction(){super(Quit);}publicvoidactionPerformed(ActionEventae){System.exit(0);}}publicclassPageNextPageActionextendsAbstractAction{privatestaticfinallongserialVersionUID=1L;publicPageNextPageAction(){super(Nextpage);}publicvoidactionPerformed(ActionEventae){if(pageRenderer!=null)pageRenderer.nextPage();showTitle();}}publicclassPagePreviousPageActionextendsAbstractAction{pr
本文标题:java打印机
链接地址:https://www.777doc.com/doc-6461935 .html