Mr bangq请教你一个java打印的问题!

06-01-22 squall
下面这段代码运行调试都没问题,问题出在,打印的时候测试文字都

打不出来,打印机直接就跳掉了,我用的打印机是Epson LQ-300k+ ESC/p 2,大家帮帮忙啊,谢谢!

import java.awt.*;

import java.awt.event.*;

import java.awt.font.*;

import java.awt.geom.*;

import java.awt.print.*;

import java.util.*;

import javax.print.*;

import javax.print.attribute.*;

import javax.swing.*;

public class PrintTest

{

public static void main(String[] args)

{

JFrame frame = new PrintTestFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.show();

}

}

class PrintTestFrame extends JFrame

{

public PrintTestFrame()

{

setTitle("PrintTest");

setSize(WIDTH, HEIGHT);

Container contentPane = getContentPane();

canvas = new PrintPanel();

contentPane.add(canvas, BorderLayout.CENTER);

attributes = new HashPrintRequestAttributeSet();

JPanel buttonPanel = new JPanel();

JButton printButton = new JButton("Print");

buttonPanel.add(printButton);

printButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

try

{

PrinterJob job = PrinterJob.getPrinterJob();

PageFormat pageFormat = job.defaultPage();//得到默认页格式

Paper paper = pageFormat.getPaper();//得到页面格式的纸张

paper.setSize(paper.getWidth(),paper.getHeight());//纸张大小

paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());

pageFormat.setPaper(paper);//将该纸张作为格式

job.setPrintable(canvas,pageFormat);//不止提供打印内容,还有格式

if (job.printDialog())

{

job.print();

}

}

catch (PrinterException exception)

{

JOptionPane.showMessageDialog(

PrintTestFrame.this, exception);

}

}

});

JButton pageSetupButton = new JButton("Page setup");

buttonPanel.add(pageSetupButton);

pageSetupButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

PrinterJob job = PrinterJob.getPrinterJob();

job.pageDialog(attributes);

}

});

JButton printPreviewButton = new JButton("Print preview");

buttonPanel.add(printPreviewButton);

printPreviewButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

PrinterJob job = PrinterJob.getPrinterJob();

PageFormat pageFormat = job.defaultPage();//得到默认页格式

Paper paper = pageFormat.getPaper();//得到页面格式的纸张

paper.setSize(paper.getWidth(),paper.getHeight());

System.out.println(paper.getWidth());

System.out.println(paper.getHeight());

paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());

pageFormat.setPaper(paper);//将该纸张作为格式

job.setPrintable(canvas,pageFormat);

PrintPreviewDialog dialog = new PrintPreviewDialog(canvas,pageFormat,1);

dialog.show();

}

});

contentPane.add(buttonPanel, BorderLayout.NORTH);

}

private PrintPanel canvas;

private PrintRequestAttributeSet attributes;

private static final int WIDTH = 500;

private static final int HEIGHT=400;

}

class PrintPanel extends JPanel implements Printable

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

drawPage(g);

}

public int print(Graphics g, PageFormat pf, int page)

throws PrinterException

{

if (page >= 1) return Printable.NO_SUCH_PAGE;

Graphics g2 = g;

g2.translate(0,0);

drawPage(g);

return Printable.PAGE_EXISTS;

}

public void drawPage(Graphics g2)

{

Font f = new Font("楷体_GB2312",Font.PLAIN, 12);

g2.setFont(f);

g2.drawString("测试一下",290,110);

g2.drawString("测试一下下",300,150);

}

}

class PrintPreviewDialog extends JDialog

{

public PrintPreviewDialog(Printable p, PageFormat pf,

int pages)

{

Book book = new Book();

book.append(p, pf, pages);

layoutUI(book);

}

public PrintPreviewDialog(Book b)

{

layoutUI(b);

}

public void layoutUI(Book book)

{

setSize(WIDTH, HEIGHT);

Container contentPane = getContentPane();

canvas = new PrintPreviewCanvas(book);

contentPane.add(canvas, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();

JButton nextButton = new JButton("Next");

buttonPanel.add(nextButton);

nextButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

canvas.flipPage(1);

}

});

JButton previousButton = new JButton("Previous");

buttonPanel.add(previousButton);

previousButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

canvas.flipPage(-1);

}

});

JButton closeButton = new JButton("Close");

buttonPanel.add(closeButton);

closeButton.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent event)

{

setVisible(false);

}

});

contentPane.add(buttonPanel, BorderLayout.SOUTH);

}

private PrintPreviewCanvas canvas;

private static final int WIDTH = 300;

private static final int HEIGHT = 300;

}

class PrintPreviewCanvas extends JPanel

{

public PrintPreviewCanvas(Book b)

{

book = b;

currentPage = 0;

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

PageFormat pageFormat = book.getPageFormat(currentPage);

double xoff; // x offset of page start in window

double yoff; // y offset of page start in window

double scale; // scale factor to fit page in window

double px = pageFormat.getWidth();

double py = pageFormat.getHeight();

double sx = getWidth() - 1;

double sy = getHeight() - 1;

if (px / py < sx / sy) // center horizontally

{

scale = sy / py;

xoff = 0.5 * (sx - scale * px);

yoff = 0;

}

else // center vertically

{

scale = sx / px;

xoff = 0;

yoff = 0.5 * (sy - scale * py);

}

g2.translate((float)xoff, (float)yoff);

g2.scale((float)scale, (float)scale);

// draw page outline (ignoring margins)

Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);

g2.setPaint(Color.white);

g2.fill(page);

g2.setPaint(Color.black);

g2.draw(page);

Printable printable = book.getPrintable(currentPage);

try

{

printable.print(g2, pageFormat, currentPage);

}

catch (PrinterException exception)

{

g2.draw(new Line2D.Double(0, 0, px, py));

g2.draw(new Line2D.Double(0, px, 0, py));

}

}

public void flipPage(int by)

{

int newPage = currentPage + by;

if (0 <= newPage && newPage < book.getNumberOfPages())

{

currentPage = newPage;

repaint();

}

}

private Book book;

private int currentPage;

}

猜你喜欢