对于Java的打印,可以用两个字来形容:“恶心”
解决方案 »
- 初学java,弱弱的问一下,类名.class是啥意思?
- XP系统上Socket发送UDP包正常,但是运行在WIN7上就会出现问题
- 怎么把一个applet改为application,以这个applet为例子
- 一个小问题,关于main线程和child线程的执行顺序问题,多谢!
- .metadata文件夹是什么?
- 老师,状态栏里怎么显示 鼠标当前位置
- lucene可以对数据库进行全文查询吗?
- 如何用java实现通过串口发短信
- java监听鼠标和键盘的全局事件的源代码!不要在框架面板内,就是在桌面上的代码!
- 为什么少取一个元素?-棘手!
- applet的中文显示。全部分数都给你,只有这么多了。
- 高手指点!谢谢了!
我进去以后,老是在检测我的JVM怎么回事?要多长时间?我的JVM已经安装了啊
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
import javax.swing.table.*;public class SimpleBook extends JPanel implements ActionListener{
private int mNumPagesX;
private int mNumPagesY;
private int mNumPages;
public double originX = 0;
public double originY = 0;
public JTable table ;
public JTableHeader head;
public Paper p;
final static JButton button = new JButton("Print"); public SimpleBook(JTable a) {
table = a;
head = table.getTableHeader();
button.addActionListener(this);
}
public void setNum(PageFormat pageformat)
{
double head_height = head.getHeaderRect(0).getHeight();
double rowheight = table.getRowHeight();
int rowcount = table.getRowCount();
int rowsofpages = (int)((pageformat.getImageableHeight()-head_height)/rowheight);//count rows of page that i want;
double imageableheight = rowsofpages*rowheight+head_height; //set imageableheight of paper
p = pageformat.getPaper();
double x = p.getImageableX();
double y = p.getImageableY();
double imageablewidth = p.getImageableWidth();
p.setImageableArea(x,y,imageablewidth,imageableheight);
pageformat.setPaper(p);
//get the number for page;
mNumPagesY = rowcount/rowsofpages;
if((rowcount%rowsofpages)>0)
mNumPagesY++;
Rectangle tangle = table.getBounds(null);
double width = tangle.width;
mNumPagesX = (int) ((width + pageformat.getImageableWidth() - 1)/ pageformat.getImageableWidth());
mNumPages = mNumPagesX * mNumPagesY;
}
public void actionPerformed(ActionEvent e)
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageformat = job.defaultPage();
setNum(pageformat);
System.out.println("yuan y"+pageformat.getImageableY());
Book bk = new Book();
for(int i=0;i<mNumPages;i++)
{
bk.append(new PaintCover(), pageformat);
}
// Pass the book to the PrinterJob
job.setPageable(bk);
// Put up the dialog box
if (job.printDialog())
{
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception exc) { /* Handle Exception */ }
}
}
class PaintCover implements Printable
{
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
int x = pageIndex % mNumPagesX;
int y = pageIndex /mNumPagesX;
originX = (pageFormat.getImageableWidth())*x;
originY = (pageFormat.getImageableHeight()-head.getHeaderRect(0).getHeight())*y;
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()+head.getHeaderRect(0).getHeight());
g2.translate(-originX,-originY);
table.paint(g2.create(0,0,(int)(originX+pageFormat.getImageableWidth()),(int)(originY+pageFormat.getImageableHeight()-head.getHeaderRect(0).getHeight())));
g2.dispose();
g2 = (Graphics2D) graphics;
g2.translate(0, -head.getHeaderRect(0).getHeight());
g2.translate(0, originY);
head.paint(g2.create(0,0,(int)(originX+pageFormat.getImageableWidth()),(int)head.getHeaderRect(0).getHeight()));
g2.dispose();
return PAGE_EXISTS;
}
}
}
再次感谢路人甲。