Mr bangq請教你一個java列印的問題!

squall發表於2006-01-22
下面這段程式碼執行除錯都沒問題,問題出在,列印的時候測試文字都
打不出來,印表機直接就跳掉了,我用的印表機是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;
}

相關文章