java文本打印机源码含打印预览

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JFileChooser;
PDF created with pdfFactory Pro trial version
public void showTitle() {
PDF created with pdfFactory Pro trial version
int currentPage = pageRenderer.getCurrentPage() + 1; int numPages = pageRenderer.getNumPages(); setTitle(title + " - page " + currentPage + " of " + numPages); }
public FilePrintAction(){ super("Print"); }
public void actionPerformed(ActionEvent ae){ PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(pageRenderer, pageFormat); if (pj.printDialog()){ try{ pj.print(); } catch (PrinterException e){
PDF created with pdfFactory Pro trial version
System.out.println(e); } } } }
public class FilePageSetupAction extends AbstractAction { private static final long serialVersionUID = 1L;
PDF created with pdfFactory Pro trial version
file.addSeparator(); file.add(new FileQuitAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q ,
Event.CTRL_MASK)); mb.add(file); JMenu page = new JMenu("Page", true); page.add(new PageNextPageAction()) .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE _DOWN, 0)); page.add(new PagePreviousPagetroke.getKeyStroke(KeyEvent.VK_PAGE _UP, 0)); mb.add(page); setJMenuBar(mb); getContentPane().setLayout(new BorderLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Event; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; 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 java.awt.print.PrinterJob; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Vector;
import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.KeyStroke;
public class PrinterDemo extends JFrame{ private static final long serialVersionUID = 1L; private PageFormat private FilePageRenderer private String public PrinterDemo(){ super(); init(); PrinterJob pj = PrinterJob.getPrinterJob(); pageFormat = pj.defaultPage(); setVisible(true); } protected void init() { setSize(350, 300); center(); Container content = getContentPane(); content.setLayout(new BorderLayout()); JMenuBar mb = new JMenuBar(); JMenu file = new JMenu("File", true); file.add(new FileOpenAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O ,Event.CTRL_MASK)); file.add(new FilePrintPreviwAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEve nt.VK_V, Event.ALT_MASK)); file.add(new FilePrintAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ P,Event.CTRL_MASK)); file.add(new FilePageSetupAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_P, Event.CTRL_MASK| Event.SHIFT_MASK)); pageFormat; pageRenderer; title;
public class FilePrintPreviwAction extends AbstractAction{ private static final long serialVersionUID = 1L;
public FilePrintPreviwAction(){ super("PrintPreviw"); }
protected void center() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); int x = (screenSize.width - frameSize.width) / 2; int y = (screenSize.height - frameSize.height) / 2; setLocation(x, y); }
public void actionPerformed(ActionEvent ae){ new PrintPreview(pageRenderer,"PrintPreviw",pageFormat.getOrientation()); } }
public class FilePrintAction extends AbstractAction{ private static final long serialVersionUID = 1L;
猪哥解答: 我这里有以前收藏的代码,两个类实现了简易的文本打印机的功能,包括预览。 简单跟你说一下。 1、PrinterDemo.java 主体类,也是入口类,里面有 main 方法可以直接在 Eclipse 中调试运行,他实现了从本地磁盘读取文本类文件打印以及打印预览的功能, 其 中 File 动作按钮中的 PrintPreviw 就是打印预览功能,你可以运行看看。 2、PrintPreview.java 打印预览类,这是专门为预览打印设计的类,通过他的构造 方法可以构造出一个预览类,PrinterDemo 中的预览功能就是调用了这个类。 两个类的源码: 【PrinterDemo.java】的源码:
PDF created with pdfFactory Pro trial version
} catch (java.io.IOException ioe) { System.out.println(ioe); } } }
public static void main(String[] args){ new PrinterDemo(); }
public class FileOpenAction extends AbstractAction { private static final long serialVersionUID = 1L;
public FileOpenAction() { super("Open..."); }
public void actionPerformed(ActionEvent ae) { // Pop up a file dialog. JFileChooser fc = new JFileChooser("."); int result = fc.showOpenDialog(PrinterDemo.this); if (result != 0) { return; } java.io.File f = fc.getSelectedFile(); if (f == null) { return; } // Load the specified file. try { pageRenderer = new FilePageRenderer(f, pageFormat); title = "[" + f.getName() + "]"; showTitle(); JScrollPane jsp = new JScrollPane(pageRenderer); getContentPane().removeAll(); getContentPane().add(jsp, BorderLayout.CENTER); validate();
相关文档
最新文档