在 JTable 之前和之后附加用于打印的文本 [英] Append text for printing before and after a JTable

查看:23
本文介绍了在 JTable 之前和之后附加用于打印的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印 JTable 并且 print() 方法在我遇到这种情况之前效果很好.假设我想打印之前,仅在第一页(不是标题)中打印文本报告",最后打印文本这是报告的结尾".我想再次澄清一下,当我打印它们时,我不需要页眉或页脚,仅此文本会出现在最后一页的顶部和底部.

我该怎么做?

解决方案

一种方法是将append()一系列合适的Printable实例添加到java.awt.print.Book,如此处所示.>

附录:JTable 有一个 getPrintable() 应该简化事情的方法;这是一个大纲和简单的标题Printable:

PrinterJob pj = PrinterJob.getPrinterJob();Book book = new Book();book.append(new Title(), pj.defaultPage());book.append(table.getPrintable(...), pj.defaultPage());book.append(new EndPage(), pj.defaultPage());pj.setPageable(书);pj.print();...私有静态类 Title 实现了 Printable {Font font = new Font("SansSerif", Font.PLAIN, 48);@覆盖公共 int 打印(图形 g,PageFormat pf,int pageIndex)抛出 PrinterException {Graphics2D g2d = (Graphics2D) g;g2d.translate(pf.getImageableX(), pf.getImageableY());g2d.setFont(字体);g2d.setColor(Color.black);g2d.drawString("报告", 50, 200);返回 Printable.PAGE_EXISTS;}}

I am trying to print a JTable and the print() method works great till I come to this scenario. Lets say I want to print before, in the first page only (not header) the text "Report" and on the end the text "This is the end of report". I would like once more to clarify that I don't need a header or footer only this text to appear in the top of the first and bottom of the last page when I print them.

How can I do this?

解决方案

One way to do this is to append() a series of suitable Printable instances to a java.awt.print.Book, as shown here.

Addendum: JTable has a getPrintable() method that should simplify things; here's an outline and simple title Printable:

PrinterJob pj = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new Title(), pj.defaultPage());
book.append(table.getPrintable(...), pj.defaultPage());
book.append(new EndPage(), pj.defaultPage());
pj.setPageable(book);
pj.print();
...
private static class Title implements Printable {

    Font font = new Font("SansSerif", Font.PLAIN, 48);

    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException {
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g2d.setFont(font);
        g2d.setColor(Color.black);
        g2d.drawString("Report", 50, 200);
        return Printable.PAGE_EXISTS;
    }
}

这篇关于在 JTable 之前和之后附加用于打印的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆