gpt4 book ai didi

java - MessageFormat header/footerFormat 如何更改 JTable 打印的字体

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:18 25 4
gpt4 key购买 nike

关于这个thread我有一个问题,是否有人知道是否可以为 MessageFormat headerFormat 覆盖/更改更大的字体(字体类型、大小、颜色)附带 JTable.PrintMode或者我必须分别绘制 g2.drawString("my header/footer") 和 JTable#print()

最佳答案

正如大家已经提到的(当我在假期放松时:-) - TablePrintable 为保密而紧密编织,无法子类化,无法配置页眉/页脚打印。 Hook 的唯一选择是包装表格的默认可打印文件,让它在没有页眉/页脚的情况下完成工作,并接管页眉/页脚自己打印。

到目前为止显示的片段的问题是它们不能很好地处理多页 - 当然正如所有作者所知和提到的 - 因为默认的可打印文件认为没有页眉/页脚并自由使用所需的空间被他们。不足为奇:-)

所以问题是:有没有办法让默认不打印到页眉/页脚区域?是的,它是:double-wopper (ehh .. wrapper) 是答案 - 通过将给定的 pageFormat 包装成一个返回调整后的 getImageableHeight/Y 的页面,使默认可打印相信它具有更少的可打印空间。像这样的东西:

public class CustomPageFormat extends PageFormat {

private PageFormat delegate;
private double headerHeight;
private double footerHeight;

public CustomPageFormat(PageFormat format, double headerHeight, double footerHeight) {
this.delegate = format;
this.headerHeight = headerHeight;
this.footerHeight = footerHeight;
}
/**
* @inherited <p>
*/
@Override
public double getImageableY() {
return delegate.getImageableY() + headerHeight;
}

/**
* @inherited <p>
*/
@Override
public double getImageableHeight() {
return delegate.getImageableHeight() - headerHeight - footerHeight;
}

// all other methods simply delegate

然后在可打印包装器中使用(页脚必须以类似方式完成):

public class CustomTablePrintable implements Printable {

Printable tablePrintable;
JTable table;
MessageFormat header;
MessageFormat footer;

public CustomTablePrintable(MessageFormat header, MessageFormat footer) {
this.header = header;
this.footer = footer;
}

public void setTablePrintable(JTable table, Printable printable) {
tablePrintable = printable;
this.table = table;
}

@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
// grab an untainted graphics
Graphics2D g2d = (Graphics2D)graphics.create();
// calculate the offsets and wrap the pageFormat
double headerOffset = calculateHeaderHeight(g2d, pageIndex);
CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
// feed the wrapped pageFormat into the default printable
int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
g2d.dispose();
return exists;
}
// translate the graphics to the start of the original pageFormat and draw header
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
printHeader(g2d, pageIndex, (int) pageFormat.getImageableWidth());
g2d.dispose();

return PAGE_EXISTS;
}


protected double calculateHeaderHeight(Graphics2D g, int pageIndex) {
if (header == null) return 0;
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
return rect.getHeight();
}

protected void printHeader(Graphics2D g, int pageIndex, int imgWidth) {
Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
String text = header.format(pageNumber);
Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
g.setFont(headerFont);
Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
// following is c&p from TablePrintable printText
int tx;

// if the text is small enough to fit, center it
if (rect.getWidth() < imgWidth) {
tx = (int) ((imgWidth - rect.getWidth()) / 2);

// otherwise, if the table is LTR, ensure the left side of
// the text shows; the right can be clipped
} else if (table.getComponentOrientation().isLeftToRight()) {
tx = 0;

// otherwise, ensure the right side of the text shows
} else {
tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
}

int ty = (int) Math.ceil(Math.abs(rect.getY()));
g.setColor(Color.BLACK);
g.drawString(text, tx, ty);

}
}

最后从表的 getPrintable 中返回它,例如:

    final JTable table = new JTable(myModel){

/**
* @inherited <p>
*/
@Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat, MessageFormat footerFormat) {
Printable printable = super.getPrintable(printMode, null, null);
CustomTablePrintable custom = new CustomTablePrintable(headerFormat, footerFormat);
custom.setTablePrintable(this, printable);
return custom;
}

};

printHeader/Footer 可以实现以执行任何需要的操作。

归根结底:“我是否需要调用 g.drawString(...)”这个问题的答案仍然是"is"。但至少它在 table 本身之外是安全的:-)

关于java - MessageFormat header/footerFormat 如何更改 JTable 打印的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6144775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com