(转贴)
PrintJob、Printable类:
打印主要通过PrintJob类的print的回调函数来实现打印的全部工作。PrintJob类是抽象类,不能new一个实例,但通过它的一个静态方法getPrintJob就可以得到一个PrintJob句柄。有了这样一个操纵杆,我们可以随时打开开关,叫它“快给我打印!”(print),如果这样,它会问你:“叫我打印什么呢?”,即回调你的打印内容,如果你没有打印的内容,它会抛出一句话:“没有东西嘛!”(PrintException)。否则它就会忠实的根据需求打印。
所以你应该提供一个可打印的内容(Printable)。只有一个方法需要你实现:
print(Graphics g , PageFormat pf , int pageIndex );
你所有的打印控制其实都是在该方法中实现。提供一个最简单的例子:
import java.awt.Graphics;
import java.awt.print.*;public class TestPrint {
JTextArea JTextArea1 = new JTextArea("sadfsdf");
public static void main(String[] args) {
PrintJob job = PrintJob.getPrintJob();//获得句柄。
job.setPrintable(new Hello());//告诉job到哪里找打印内容。
try{
job.print();//快给我打印!
}catch(PrintException e) {
System.out.println(“没有东西嘛!”);//或打印机硬件问题。
}
} class Hello implements Printable {//可打印的内容。
public int print(Graphics g , //可以任意画打印的内容的白纸。
PageFormat pf , // 打印格式,缺省。
int pageIndex ) {// 页码,从0开始。
if(pageIndex != 0 )return NO_SUCH_PAGE;//停止打印。
g.drawString(jTextArea1.getText(),100,100);//在纸上画字符串。
return PAGE_EXITS;//继续打印。
}
}
}
PrintJob、Printable类:
打印主要通过PrintJob类的print的回调函数来实现打印的全部工作。PrintJob类是抽象类,不能new一个实例,但通过它的一个静态方法getPrintJob就可以得到一个PrintJob句柄。有了这样一个操纵杆,我们可以随时打开开关,叫它“快给我打印!”(print),如果这样,它会问你:“叫我打印什么呢?”,即回调你的打印内容,如果你没有打印的内容,它会抛出一句话:“没有东西嘛!”(PrintException)。否则它就会忠实的根据需求打印。
所以你应该提供一个可打印的内容(Printable)。只有一个方法需要你实现:
print(Graphics g , PageFormat pf , int pageIndex );
你所有的打印控制其实都是在该方法中实现。提供一个最简单的例子:
import java.awt.Graphics;
import java.awt.print.*;public class TestPrint {
JTextArea JTextArea1 = new JTextArea("sadfsdf");
public static void main(String[] args) {
PrintJob job = PrintJob.getPrintJob();//获得句柄。
job.setPrintable(new Hello());//告诉job到哪里找打印内容。
try{
job.print();//快给我打印!
}catch(PrintException e) {
System.out.println(“没有东西嘛!”);//或打印机硬件问题。
}
} class Hello implements Printable {//可打印的内容。
public int print(Graphics g , //可以任意画打印的内容的白纸。
PageFormat pf , // 打印格式,缺省。
int pageIndex ) {// 页码,从0开始。
if(pageIndex != 0 )return NO_SUCH_PAGE;//停止打印。
g.drawString(jTextArea1.getText(),100,100);//在纸上画字符串。
return PAGE_EXITS;//继续打印。
}
}
}
解决方案 »
- java中左移高位溢出不舍去么?还有没有节操啦
- notify问题
- input一个日期,计算出下一个月的最后一天
- 什么叫 Java 不提供类的多重继承,但提供接口多重继承机制?
- 请教如何制作操作界面来传递文件路径
- 急! 在线等!高分回报 !
- 大家好,我这里有80个Gmail邮箱的邀请,请留下你的电邮,我发邮件邀请你,谢谢
- 如何应用AWT在本地生成图片(任何格式的图片)
- 急:希望在刷新和关闭IE窗口时让applet做不同的事情,怎么办?
- 关于jdbTable的插入问题
- 什么问题:Exception in thread "main" java.lang.NoClassDefFoundError:test(runtime)
- 给分:lifeis(人就是直立行走的驴) 请进
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.text.*;
/**
* The PrintText application expands on the
* PrintExample application in that it images
* text on to the single page printed.
*/
public class PrintText implements Printable {
/**
* The text to be printed.
*/
private static final String mText =
"Four score and seven years ago our fathers brought forth on this "
+ "continent a new nation, conceived in liberty and dedicated to the "
+ "proposition that all men are created equal. Now we are engaged in "
+ "a great civil war, testing whether that nation or any nation so "
+ "conceived and so dedicated can long endure. We are met on a great "
+ "battlefield of that war. We have come to dedicate a portion of "
+ "that field as a final resting-place for those who here gave their "
+ "lives that that nation might live. It is altogether fitting and "
+ "proper that we should do this. But in a larger sense, we cannot "
+ "dedicate, we cannot consecrate, we cannot hallow this ground."
+ "The brave men, living and dead who struggled here have consecrated "
+ "it far above our poor power to add or detract. The world will "
+ "little note nor long remember what we say here, but it can never "
+ "forget what they did here. It is for us the living rather to be "
+ "dedicated here to the unfinished work which they who fought here "
+ "have thus far so nobly advanced. It is rather for us to be here "
+ "dedicated to the great task remaining before us--that from these "
+ "honored dead we take increased devotion to that cause for which "
+ "they gave the last full measure of devotion--that we here highly "
+ "resolve that these dead shall not have died in vain, that this "
+ "nation under God shall have a new birth of freedom, and that "
+ "government of the people, by the people, for the people shall "
+ "not perish from the earth.";
/**
* Our text in a form for which we can obtain a
* AttributedCharacterIterator.
*/
private static final AttributedString mStyledText = new AttributedString(mText);
/**
* Print a single page containing some sample text.
*/
static public void main(String args[]) {
/* Get the representation of the current printer and
* the current print job.
*/
PrinterJob printerJob = PrinterJob.getPrinterJob();
/* Build a book containing pairs of page painters (Printables)
* and PageFormats. This example has a single page containing
* text.
*/
Book book = new Book();
book.append(new PrintText(), new PageFormat());
/* Set the object to be printed (the Book) into the PrinterJob.
* Doing this before bringing up the print dialog allows the
* print dialog to correctly display the page range to be printed
* and to dissallow any print settings not appropriate for the
* pages to be printed.
*/
printerJob.setPageable(book);
/* Show the print dialog to the user. This is an optional step
* and need not be done if the application wants to perform
* 'quiet' printing. If the user cancels the print dialog then false
* is returned. If true is returned we go ahead and print.
*/
boolean doPrint = printerJob.printDialog();
if (doPrint) {
try {
printerJob.print();
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
}
}
/**
* Print a page of text.
*/
public int print(Graphics g, PageFormat format, int pageIndex) {
/* We'll assume that Jav2D is available.
*/
Graphics2D g2d = (Graphics2D) g;
/* Move the origin from the corner of the Paper to the corner
* of the imageable area.
*/
g2d.translate(format.getImageableX(), format.getImageableY());
/* Set the text color.
*/
g2d.setPaint(Color.black);
/* Use a LineBreakMeasurer instance to break our text into
* lines that fit the imageable area of the page.
*/
Point2D.Float pen = new Point2D.Float();
AttributedCharacterIterator charIterator = mStyledText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
float wrappingWidth = (float) format.getImageableWidth();
while (measurer.getPosition() < charIterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += layout.getAscent();
float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
layout.draw(g2d, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading();
}
return Printable.PAGE_EXISTS;
}
}
上面的程序是有点错类名写错了应该是PrinterJob Not PrintJob
这下你再试试,在Jbuilder下新建一个Project untitled13
运行就可以了,注意纸是A3的