1

我目前正在使用它来打印我的表格,并且它有效。但我对消息格式的布局并不满意,我希望页脚和日期都在页脚中,日期格式与表格左侧对齐,页面在右侧。

我怎样才能做到这一点?一直在阅读一些关于覆盖 PrintTable 方法的内容,但从我所读到的内容似乎变得相当复杂。

希望你能帮我解决这个问题,谢谢。:)

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.JTable;
import dk.beesys.rims.ui.WindowInventory;


public class Print {

   private static Print INSTANCE;

   public static Print getInstance() {
      if (INSTANCE == null) {
         INSTANCE = new Print();
      }
      return INSTANCE;
   }

private Print(){ }

    public void printList(java.awt.event.ActionEvent ignore) {
       String strDate = MessageFormat.format("{0,date,short} {0,time,short}", new Date());

        MessageFormat header = new MessageFormat("- {0} -"); 
        MessageFormat footer = new MessageFormat("Printed: " + strDate);
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);

        try {

            WindowInventory.getInstance().getTable().print(JTable.PrintMode.FIT_WIDTH, header, footer, true, aset, true);

        } catch (java.awt.print.PrinterException e) {
            System.err.format("Cannot print %s%n", e.getMessage());
        }
    }
4

2 回答 2

3

你可以看看这个CustomTablePrintable。你喂它你的桌子的朴素的getPrintable()结果。在您的PrinterJob中,自定义print()将生成表格图像,然后在相同的图形上下文中绘制您的页脚。您可以使用上下文的边界,getFontMetrics()stringWidth()确定在哪里绘制格式化字符串。

附录:这是在每页右下角打印灰色日期的示例:

public int print(Graphics g, PageFormat pf, int index) throws PrinterException {
    if (index > 0) return NO_SUCH_PAGE;
    String s = new Date().toString();
    Graphics2D g2d = (Graphics2D) g;
    table.getPrintable(
        JTable.PrintMode.NORMAL, null, null).print(g2d, pf, index);
    Rectangle r = g2d.getClipBounds();
    int dw = g2d.getFontMetrics().stringWidth(s);
    int dh = g2d.getFontMetrics().getHeight();
    System.out.println(index + " " + r);
    g2d.setPaint(Color.gray);
    g2d.drawString(s, r.x + r.width - dw, r.y + r.height - dh);
    return Printable.PAGE_EXISTS;
}

附录:虽然这种方法适用于适合单页的表格,但本文描述了打印大于一页的组件

于 2010-02-22T19:12:39.077 回答
0

现在一直在尝试一些事情,但仍然无法得到任何工作。我知道我在这里粘贴的内容可能有点(方式)偏离,但这是尝试各种事情的结果。

public class CustomTablePrint implements Printable  {

private static CustomTablePrint INSTANCE = null;

public static CustomTablePrint getInstance() {
    if (INSTANCE == null) {
        INSTANCE = new CustomTablePrint();
    }
    return INSTANCE;
}

private CustomTablePrint() {}


    @Override


    public int print(Graphics graphics, PageFormat pageFormat,  
            int pageIndex) throws PrinterException { 
        if (pageIndex > 0) { 
            return NO_SUCH_PAGE; 
        } 
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(WindowInventory.getInstance().getTable().getPrintable(JTable.PrintMode.FIT_WIDTH, null, null));



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

        // Draw header/footer here

        String strDate = MessageFormat.format("{0,date,short} {0,time,short}", new Date());
        String printed = "Printed: ";

        graphics.drawString(printed + strDate, 10, 10); 

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);
        job.printDialog(aset);

        return PAGE_EXISTS;         
    } 
}
于 2010-02-23T10:51:50.447 回答