当前位置: 首页 > 面试题库 >

MessageFormat标头/ footerFormat如何更改JTable打印的字体

都博裕
2023-03-14
问题内容

关于这个主题我有一个问题,如果有人知道是否可以覆盖/变化较大的字体(字体类型,大小,颜色),用于MessageFormat中headerFormat缺憾与JTable.PrintMode或我必须画g2.drawString(“我的头/页脚”)和JTable#print()分别


问题答案:

正如每个人都已经提到的那样(当我在假期放松时:-)-TablePrintable紧密结合以确保机密性,无法继承任何类,也无法配置页眉/页脚打印。挂钩的唯一选择是包装表的默认可打印内容,让它在没有页眉/页脚的情况下进行工作,并自行打印页眉/页脚。

到目前为止显示的代码片段的问题在于,它们不能很好地与多页一起使用-当然,这是所有作者都知道和提到的-
因为默认可打印内容认为没有页眉/页脚,并且可以自由使用它们所需的空间。不出所料 :-)

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

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(…)”这个问题的答案仍然是“是”。但是至少它安全地位于表本身之外:-)



 类似资料:
  • 问题内容: 我想打印这样的东西 图片 标题 捷普 我知道要在messageFormat标头中打印带有标题的jtable,但我不知道如何添加图像 图像是徽标 问题答案: 您可以尝试此处显示的方法。使用而不是在您的可打印物中。

  • 问题内容: 我看了文件。 但我想我一定误会了。 我也尝试过 我想更改为 都不起作用。 问题答案: 使用JSONP时,无法控制浏览器发送的标头。JSONP是一个聪明的把戏(或者是一个hack,具体取决于您对它的看法…),其中包括插入指向服务器端点的标记。最终,这是一个浏览器,它会在通过标签请求脚本时决定要发送的标头,并且您不能影响它。

  • 问题内容: 使用以下示例代码: 我们想用上面的代码在column_names中设置带有列名的标头,但是它不起作用。尽管正在创建表,但标题不可见。 问题答案: 为了能够看到标题,您应该将表放在JScrollPane中。 或者,如果您确实不希望使用滚动窗格,则可以将tableHeader专门添加到面板中(但是:通常,您不希望这种行为):

  • 问题内容: 我试过了: 不起作用 编辑:此代码仅在我的项目中不起作用。在其他项目中工作。我可能已经更改了阻止颜色更改的属性。也许NetBeans具有一些保留默认颜色的属性。我注意到了别的东西。我项目中标题的颜色以不同的方式闪烁。在可以进行颜色更改的示例中,我看到了不同的图形。 编辑2:其他。我注意到按钮也不会改变颜色。必须是通用的。希望这可以帮助。不幸的是,SSCCE在这种情况下不起作用,因为我无

  • 如果只是将JTable添加到JPanel中,默认情况下它不会显示头部。您应该将该表传递给JScrollPane的构造函数,或者使用该表作为参数调用JScrollPane。那么实际上,是什么使列标题可见呢?它是JScrollPane(和方法)内部呈现的一部分吗? 最初我认为JScrollPane使用它的来实现这一点,但它没有(将null传递给这个方法,表仍然会显示头部)。

  • 有没有办法更改中3个带圆圈的表格标题的背景色? 我还想将所有3个带圆圈的列的字体颜色更改为红色。 下面是我桌子的图像。谢谢