从书上抄来的,仅供参考一下
Tookit kt=Toolkit.getDefaultTookit();
PrintJob job=tk.getPrintJob(this,"title",null);
Graphics g=job.getGraphics;
panel1,printAll(g);
g.dispose();
job.end();
java打印时,首先创建一个PrintJob对象,在java.awt.Tookit中有这样的方法可以得到PrintJob的实例:
public abstract PrintJob getPrintJob(Frame parent,String jobname,Properties props);
进行打印工作时,必须与一个Frame的实例相联系,得到一个PrintJob对象后,就可以调用PrintJob对象的方法getGraphics来打印所需要打印的各个页面,
public abstract Graphics getGraphics();
每个Graphics的新实例代表一个新的单独打印页,打印完所有的页面后,可以调用PrintJob中的end方法来完成打印工作
Tookit kt=Toolkit.getDefaultTookit();
PrintJob job=tk.getPrintJob(this,"title",null);
Graphics g=job.getGraphics;
panel1,printAll(g);
g.dispose();
job.end();
java打印时,首先创建一个PrintJob对象,在java.awt.Tookit中有这样的方法可以得到PrintJob的实例:
public abstract PrintJob getPrintJob(Frame parent,String jobname,Properties props);
进行打印工作时,必须与一个Frame的实例相联系,得到一个PrintJob对象后,就可以调用PrintJob对象的方法getGraphics来打印所需要打印的各个页面,
public abstract Graphics getGraphics();
每个Graphics的新实例代表一个新的单独打印页,打印完所有的页面后,可以调用PrintJob中的end方法来完成打印工作
解决方案 »
- 有关java中异常处理问题!!
- Java基础问题
- 如何用java语言实现端口扫描器
- tabel 中的关于sort的问题
- 如何禁用Jtabbedpane上的其它面板,只设置其中的一个有效?
- 从1~9中任选四个整数,如何用加减乘除,括号,求24点的游戏怎么用Java语言编啊?
- 请问::下面代码那有错误?怎么改?谢谢!
- 学习JSP需要哪方面知识?(分不多了,明天给分!回答有分)
- csdn论坛列表的浮动菜单是怎么实现的?
- JAVA数组的问题 救命啊!!!!!!!!
- 为什么有数字签名的applet还是不能够Cntr+p粘贴其他文件的内容?
- 我用JSP上传一个中文文件名的Word文件到服务器,可是上传后,文件的名字乱码,请问如果解决呢?谢谢
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.swing.*;
public class BookTest
{
public static void main(String[] args)
{
JFrame frame=new BookTestFrame();//»ù±¾µÄJAVAÓ¦ÓóÌÐò´°¿Ú£¬¿ÉÌí¼Ó×é¼þ
frame.show();
}
}
class BookTestFrame extends JFrame implements ActionListener//ActionListener½Ó¿Ú£¬ÊÜÐÅÕß½Ó¿Ú£¬ÓÃÓÚ½ÓÊܶ¯×÷ʼþ
{
public BookTestFrame()
{
setTitle("bookTest");
setSize(300,100);
addWindowListener(new WindowAdapter()
{ public void windwoClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane=getContentPane();
text=new JTextField();
contentPane.add(text,"South");
JPanel buttonPanel=new JPanel();
printButton=new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(this); pageSetupButton=new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(this); printPreviewButton=new JButton("Print prebview");
buttonPanel.add(printPreviewButton);
printPreviewButton.addActionListener(this);
contentPane.add(buttonPanel,"North");
}
public Book makeBook()
{
if (pageFormat==null)
{
PrinterJob printJob=PrinterJob.getPrinterJob();
pageFormat=printJob.defaultPage();
}
Book book=new Book();
String message=text.getText();
Banner banner=new Banner(message);
int pageCount=banner.getPageCount((Graphics2D)getGraphics(),pageFormat);
book.append(new CoverPage(message+"("+pageCount+"pages)"),pageFormat);
book.append(banner,pageFormat,pageCount);
return book;
}
public void actionPerformed(ActionEvent event)
{
Object source=event.getSource();
if(source==printButton)
{
PrinterJob printJob=PrinterJob.getPrinterJob();
printJob.setPageable(makeBook());
if(printJob.printDialog())
{
try
{
printJob.print();
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(this,exception);
}
}
}
else if (source==pageSetupButton)
{
PrinterJob printJob=PrinterJob.getPrinterJob();
if (pageFormat==null)
pageFormat=printJob.defaultPage();
pageFormat=printJob.pageDialog(pageFormat);
}
else if (source==printPreviewButton)
{
PrintPreviewDialog dialog=new PrintPreviewDialog(makeBook());
dialog.show();
}
}
private JButton printButton;
private JButton pageSetupButton;
private JButton printPreviewButton; private JTextField text;
private PageFormat pageFormat;
}
class Banner implements Printable
{
public Banner(String m)
{
message=m;
}
public int getPageCount(Graphics2D g2,PageFormat pf)
{
if(message.equals(""))return 0;
layoutPages(g2,pf);
float width=scale *layout.getAdvance();
int pages=(int)Math.ceil(width/pf.getImageableWidth());
return pages;
}
public int print(Graphics g,PageFormat pf,int page)throws PrinterException
{
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.black);
if(page>getPageCount(g2,pf))
return Printable.NO_SUCH_PAGE;
g2.translate(pf.getImageableX(),pf.getImageableY()); drawPage(g2,pf,page);
return Printable.PAGE_EXISTS;
}
{
if(message.equals(""))return;
FontRenderContext context=g2.getFontRenderContext();
Font f=new Font("Serif",Font.PLAIN,72);
g2.setFont(f);
layout=new TextLayout(message,f,context);
float ascent=layout.getAscent();
float descent=layout.getDescent();
float height=ascent+descent;
scale=(float)pf.getImageableHeight()/height;
}
public void drawPage(Graphics2D g2,PageFormat pf,int page)
{
if(message.equals(""))return;
page--;
layoutPages(g2,pf);
drawCropMarks(g2,pf);
g2.clip(new Rectangle2D.Double(0,0,pf.getImageableWidth(),pf.getImageableHeight()));
g2.translate(-page*pf.getImageableWidth(),0);
g2.scale(scale,scale);
AffineTransform transform=AffineTransform.getTranslateInstance(0,layout.getAscent());
Shape outline=layout.getOutline(transform);
g2.draw(outline);
}
public void drawCropMarks(Graphics2D g2,PageFormat pf)
{
final double C=36;
double w=pf.getImageableWidth();
double h=pf.getImageableHeight();
g2.draw(new Line2D.Double(0,0,0,0));
g2.draw(new Line2D.Double(0,0,C,0));
g2.draw(new Line2D.Double(w,0,w,0));
g2.draw(new Line2D.Double(w,0,w-C,0));
g2.draw(new Line2D.Double(0,h,0,h-C));
g2.draw(new Line2D.Double(0,h,C,h));
g2.draw(new Line2D.Double(w,h,w,h-C));
g2.draw(new Line2D.Double(w,h,w-C,h));
} private String message;
private float scale;
private float width;
private TextLayout layout;
}
class CoverPage implements Printable
{
public CoverPage(String t)
{
title=t;
}
public int print(Graphics g,PageFormat pf,int page) throws PrinterException
{
if(page>=1)return Printable.NO_SUCH_PAGE;
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.black);
g2.translate(pf.getImageableX(),pf.getImageableY());
FontRenderContext context=g2.getFontRenderContext();
Font f=g2.getFont();
TextLayout layout=new TextLayout(title,f,context);
float ascent=layout.getAscent();
g2.drawString(title,0,ascent);
return Printable.PAGE_EXISTS;
}
private String title;
}
class PrintPreviewDialog extends JDialog implements ActionListener
{
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(400,550); Container contentPane=getContentPane();
canvas=new PrintPreviewCanvas(book);
contentPane.add(canvas,"Center"); JPanel buttonPanel=new JPanel(); nextButton=new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(this); previousButton =new JButton("Previous");
buttonPanel.add(previousButton);
previousButton.addActionListener(this); closeButton=new JButton("Close");
buttonPanel.add(closeButton);
closeButton.addActionListener(this); contentPane.add(buttonPanel,"South");
}
public void actionPerformed(ActionEvent event)
{
Object source=event.getSource();
if(source==nextButton)
{
canvas.flipPage(1);
}
else if(source==previousButton)
{
canvas.flipPage(1);
}
else if(source==closeButton)
{
setVisible(false);
}
}
private JButton nextButton;
private JButton previousButton;
private JButton closeButton;
private PrintPreviewCanvas canvas;
}
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;
double yoff;
double scale;
double px=pageFormat.getWidth();
double py=pageFormat.getHeight();
double sx=getWidth()-1;
double sy=getHeight()-1;
if(px/py<sx/sy)
{
scale=sy/py;
xoff=0.5*(sx-scale*px);
yoff=0;
}
else
{
scale=sx/px;
xoff=0;
yoff=0.5*(sy-scale*py);
}
g2.translate((float)xoff,(float)yoff);
g2.scale((float)scale,(float)scale); Rectangle2D page=new Rectangle2D.Double(0,0,px,py);
g2.setPaint(Color.black);
g2.fill(page);
g2.setPaint(Color.white);
g2.fill(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;
}