java预览打印_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;
public classFilePrinterextendsJFrame{
privatePageFormat pageFormat;
privateFilePageRenderer pageRenderer;
privateString title;
publicFilePrinter() {
super();
init();
PrinterJob pj = PrinterJob();
pageFormat = pj.defaultPage();
setVisible(true);
}
protectedvoidinit() {
setSize(350,300);
center();
Container content = getContentPane();
content.setLayout(newBorderLayout());
// Add the menu bar.
JMenuBar mb =newJMenuBar();
JMenu file =newJMenu("File",true);
file.add(newFileOpenAction()).setAccelerator(
file.addSeparator();
file.add(newFileQuitAction()).setAccelerator(
JMenu page =newJMenu("Page",true);
page.add(newPageNextPageAction()).setAccelerator( KeyStroke(KeyEvent.VK_PAGE_DOWN,0)); page.add(newPagePreviousPageAction()).setAccelerator( KeyStroke(KeyEvent.VK_PAGE_UP,0));
mb.add(page);
setJMenuBar(mb);
htmlbordergetContentPane().setLayout(newBorderLayout()); addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvent e) {
}
});
}
protectedvoidcenter() {
Dimension screenSize = DefaultToolkit().getScreenSize(); Dimension frameSize = getSize();
intx =(screenSize.width - frameSize.width)/2;
inty =(screenSize.height - frameSize.height)/2;
setLocation(x, y);
}
publicvoidshowTitle() {
intcurrentPage = CurrentPage()+1;
intnumPages = NumPages();
setTitle(title +" - page "+ currentPage +" of "+ numPages);
}
public classFileOpenActionextendsAbstractAction{ publicFileOpenAction() {
super("");
}
publicvoidactionPerformed(ActionEvent ae) {
// Pop up a file dialog.
JFileChooser fc =newJFileChooser(".");
intresult = fc.showOpenDialog(FilePrinter.this);
if(result !=0) {
return;
}
java.io.File f = fc.getSelectedFile();
if(f ==null) {
return;
}
// Load the specified file.
try{
pageRenderer =newFilePageRenderer(f, pageFormat); title ="["+ f.getName()+"]";
showTitle();
JScrollPane jsp =newJScrollPane(pageRenderer); getContentPane().removeAll();
getContentPane().add(jsp, BorderLayout.CENTER); validate();
}catch(java.io.IOException ioe) {
System.out.println(ioe);
}
}
}
public staticvoidmain(String[]args) {
newFilePrinter();
}
public classFilePrintActionextendsAbstractAction{ publicFilePrintAction() {
super("Print");
}
publicvoidactionPerformed(ActionEvent ae) {
PrinterJob pj = PrinterJob();
pj.setPrintable(pageRenderer, pageFormat);
if(pj.printDialog()) {
try{
pj.print();
}catch(PrinterException e) {
System.out.println(e);
}
}
}
}
public classFilePageSetupActionextendsAbstractAction{
publicFilePageSetupAction() {
super("");
}
publicvoidactionPerformed(ActionEvent ae) {
PrinterJob pj = PrinterJob();
pageFormat = pj.pageDialog(pageFormat);
if(pageRenderer !=null) {
pageRenderer.pageInit(pageFormat);
showTitle();
}
}
}
public classFileQuitActionextendsAbstractAction{ publicFileQuitAction() {
super("Quit");
}
publicvoidactionPerformed(ActionEvent ae) {
}
}
public classPageNextPageActionextendsAbstractAction{ publicPageNextPageAction() {
super("Next page");
}
publicvoidactionPerformed(ActionEvent ae) {
if(pageRenderer !=null)
showTitle();
}
}
public classPagePreviousPageActionextendsAbstractAction{ publicPagePreviousPageAction() {
super("Previous page");
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。