彩色打印页眉和页脚? [英] Printing headers and footers in color?

查看:181
本文介绍了彩色打印页眉和页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个JTable时产生彩色页眉和页脚。具体来说,我在看getPrintable()在javax.swing.JTable中,但MessageFormat不给我指定的页眉或页脚的颜色的选项。

I am trying to create colored headers and footers when printing a JTable. Specifically, I am looking at getPrintable() in javax.swing.JTable, but MessageFormat does not give me the option to specify the color of the header or footer.

我该怎么办呢?

澄清
我感兴趣的是设置页眉/页脚打印时。例如,记事本追加文件名作为头打印的东西。

clarification I am interested in setting the header/footers while printing. For example, notepad appends the filename as a header to what you print.

更新
好像有这样做的没有标准的方式,能有人给我一些解决方法?到目前为止发布的唯一的答案有什么用印刷做(如发送到打印机,不显示屏幕)页眉/页脚。

update Seems like there is no standard way of doing this, can someone give me some workarounds? The only answer posted so far has nothing to do with printing(as in send to a printer, not displaying to screen) header/footers.

这是我的意见复制:我感兴趣的打印页眉/页脚。例如,当您打印从记事本文档,它的附加文件名作为头(或者它的页脚,我不记得确切)

Copied from my comment: I am interested in the printing header/footer. For example, when you are printing a document from notepad, it appends the filename as a header (or perhaps its the footer, I do not remember exactly)

推荐答案

一个解决方案,我能想到的就是用自己打印的:

One solution I can think of is to use your own printable:

public class CustomTablePrintable implements Printable {

    Printable tablePrintable;

    public void setTablePrintable(Printable printable) {
        tablePrintable = printable;        
    }

    public int print(Graphics graphics, PageFormat pageFormat, 
            int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        tablePrintable.print(graphics, pageFormat, pageIndex);

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Draw header/footer here
        graphics.drawString(header, posx, posy);

        return PAGE_EXISTS;        
    }
}

当你从你的JTable调用getPrintable,其注入到一个新的实例来定制打印,然后用这个用的PrinterJob。

When you call getPrintable from your JTable, inject it to a new instance to the custom printable and then use this with the PrinterJob.

您现在可以先草拟的页眉和页脚,如你所愿,但你也失去了一些东西:

You can now draw the header and footer as you wish, but you also lose some stuff:


  • 您不能使用MessageFormat格式化的消息。我相信,你可以很容易地添加此功能,您可打印。

  • 页眉和页脚不会自动定位。你可能对这种粗略的估计,但。

编辑:我看了看Java源代码,并有私有类TablePrintable做所有的工作。您可以在源$ C ​​$ C峰值看到页眉和页脚是如何被打印出来。然后,您可以将此功能转移到你的打印类。

I've looked at the Java Sources and there is the private class TablePrintable that does all the job. You can peak at the source code to see how the header and footer are printed. Then you can move this functionality to your Printable class.

这篇关于彩色打印页眉和页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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