打印表和表头的程序

zwh23 2002-04-21 02:52:22
这是一个打印表和表头的程序,我想在表头上面再加一个标题,那位大哥给我改一改,我主要是对G的移动和裁剪搞不清楚.

import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.table.*;

public class TablePrinter implements Printable, Pageable {

protected JTable table;
protected PageFormat pageFormat;
protected int headerStatus = ALL_PAGES;

/**
* These constants indicate which pages should include column headers
*/
public final static int ALL_PAGES = 0;
public final static int FIRST_PAGE_ONLY = 1;
public final static int NO_PAGES = 2;

public TablePrinter(JTable tbl, PageFormat pf) {
table = tbl;
pageFormat = pf;
}

/**
* Perform the printing here
*/
public int print(Graphics g, PageFormat pf, int index) {
Dimension size = null;
// Get the table's preferred size
if ((table.getWidth() == 0) || (table.getHeight() == 0)) {
table.setSize(table.getPreferredSize());
}
int tableWidth = table.getWidth();
int tableHeight = table.getHeight();
int positionX = 0;
int positionY = 0;

// Loop until we have printed the entire table
int pageIndex = 0;
while (positionY < tableHeight) {
positionX = 0;
while (positionX < tableWidth) {
size = getPrintSize(positionX, positionY);
if (pageIndex == index) {
// Paint as much of the table as will fit on a page
paintTable(g, positionX, positionY, size);
return Printable.PAGE_EXISTS;
}
pageIndex++;
positionX += size.width;
}
positionY += size.height;
}
return Printable.NO_SUCH_PAGE;
}

/**
* Calculate how much of the table will fit on a page without
* causing a row or column to be split across two pages
*/
protected Dimension getPrintSize(int positionX, int positionY) {
Rectangle rect;
int printWidth;
int printHeight;
int firstCol = table.columnAtPoint(
new Point(positionX, positionY));
int firstRow = table.rowAtPoint(
new Point(positionX, positionY));
int maxWidth = (int)(pageFormat.getImageableWidth());
int maxHeight = (int)(pageFormat.getImageableHeight());
if (displayHeaderOnPage(positionY)) {
maxHeight -= table.getTableHeader().getHeight();
}

int lastCol = table.columnAtPoint(
new Point(positionX + maxWidth, positionY));
if (lastCol == -1) {
printWidth = table.getWidth() - positionX;
}
else {
rect = table.getCellRect(0, lastCol - 1, true);
printWidth = rect.x + rect.width - positionX;
}

int lastRow = table.rowAtPoint(new Point(
positionX, positionY + maxHeight));
if (lastRow == -1) {
printHeight = table.getHeight() - positionY;
}
else {
rect = table.getCellRect(lastRow - 1, 0, true);
printHeight = rect.y + rect.height - positionY;
}
return new Dimension(printWidth, printHeight);
}

/**
* Paint / print a portion of the table
*/
protected void paintTable(Graphics g, int positionX, int positionY,
Dimension size) {
int offsetX = (int)(pageFormat.getImageableX());
int offsetY = (int)(pageFormat.getImageableY());
if (displayHeaderOnPage(positionY)) {
JTableHeader header = table.getTableHeader();
if ((header.getWidth() == 0) ||
(header.getHeight() == 0)) {
header.setSize(header.getPreferredSize());
}
int headerHeight = header.getHeight();
g.translate(offsetX - positionX, offsetY);
g.clipRect(positionX, 0,
size.width, size.height + headerHeight);
header.paint(g);
g.translate(0, headerHeight - positionY);
g.clipRect(positionX, positionY, size.width, size.height);
}
else {
g.translate(offsetX - positionX,
offsetY - positionY);
g.clipRect(positionX, positionY,
size.width, size.height);
}
table.paint(g);
}

/**
* Determine whether or not to paint the headers on the current page
*/
protected boolean displayHeaderOnPage(int positionY) {
return ((headerStatus == ALL_PAGES) ||
((headerStatus == FIRST_PAGE_ONLY) &&
positionY == 0));
}

/**
* Calculate the number of pages it will take to print the entire table
*/
public int getNumberOfPages() {
Dimension size = null;
int tableWidth = table.getWidth();
int tableHeight = table.getHeight();
int positionX = 0;
int positionY = 0;

int pageIndex = 0;
while (positionY < tableHeight) {
positionX = 0;
while (positionX < tableWidth) {
size = getPrintSize(positionX, positionY);
positionX += size.width;
pageIndex++;
}
positionY += size.height;
}
return pageIndex;
}

public Printable getPrintable(int index) {
return this;
}

public PageFormat getPageFormat(int index) {
return pageFormat;
}

}
...全文
45 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧