打印大型Swing组件 [英] Printing a large Swing component

查看:109
本文介绍了打印大型Swing组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swing表单,其中包含一个JScrollPane中的自定义表(它只是一个JPanel,而不是一个JTable子类),我试图让它打印出来。如果我只是将整个框架发送到打印机,滚动窗格会切断,如果我将框架的大小调整为滚动窗格内容的大小,某种内部屏障会阻止JFrame变得超过1100像素高。

I have a Swing form with a custom table inside a JScrollPane (it's just a JPanel, not a JTable subclass), and I am trying to get it to print. If I just send the whole frame to the printer, the scroll pane cuts off, and if I resize the frame to the size of the contents of the scroll pane, some sort of internal barrier stops the JFrame becoming more than about 1100 pixels tall.

另一种方法是创建对话框的内容窗格而不将其附加到根JFrame,因为JPanel的大小在这种情况下不受限制。但是为了让组件自行调整并调整到适当的大小,我似乎需要使面板可显示,这意味着至少将它添加到JFrame并调用JFrame.pack(),但同样,1100像素限制回来了。

Another alternative would be to create the content pane of the dialog without attaching it to the root JFrame, because the JPanel's size is not limited in that case. But to get the components to lay themselves out and resize to their proper sizes, I seem to need to make the panel displayable, which means at the very least adding it to a JFrame and calling JFrame.pack(), but again, the 1100 pixel limit comes back.

这是我打印组件的代码:

Here's my code for printing the component:

public static void print(final Component comp) {
    final float SCALE = .5f;
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new Printable() {
        public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException
        {
            if (page * pf.getImageableHeight() >= SCALE * comp.getHeight())
                return NO_SUCH_PAGE;
            ((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY()
               - page * pf.getImageableHeight());
            ((Graphics2D)g).scale(SCALE, SCALE);
            comp.printAll(g);
            return PAGE_EXISTS;
        }
    });
    if (job.printDialog())
        try { job.print(); }
        catch (PrinterException ex) {}
}

如果我这样做,该组件的大小为零:

If I do this, the component has zero size:

JPanel c = createPanel(); // This JPanel has a JScrollPane in it with its
                          // preferredSize equal to that of its viewport component
                          // (which is not what I do to show the dialog normally)
print(c);

如果我这样做,组件的大小合适,但打印为纯灰色,因为组件尚未布局:

If I do this, the component has the right size, but prints as solid gray because the sub-components have not been laid out:

JPanel c = createPanel();
c.setSize(c.getPeferredSize());
print(c);

这些似乎没有什么区别:

These don't seem to make a difference:

JPanel c = createPanel();
c.validate();
c.revalidate();
c.repaint();
print(c);

这使得面板变大,但它停在大约一页半(1100px):

This makes the panel larger, but it stops at about a page and a half large (1100px):

JPanel c = createPanel();
JFrame f = new JFrame();
f.setContentPane(c);
f.pack();
print(c);

我这里的排列已经不多了。有没有人知道(a)如何更改操作系统最大帧大小,(b)如何布局和绘制离屏组件,或(c)如何直接打印Swing组件,而不必绘制它(?) 。感谢帮助。

I'm running out of permutations here. Does anyone know either (a) how to change the OS maximum frame size, (b) how to layout and paint an off-screen component, or (c) how to print a Swing component directly, without having to paint it (?). Help is appreciated.

推荐答案

由于JScrollPane中的组件可以具有任意大小,即使它可以显示,我的解决方案是试试这个:

Since a component in a JScrollPane can have arbitrary size, even after it is made displayable, My solution is to try this:

JPanel c = createPanel();
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(c));
f.pack();
print(c);

这样我可以验证JPanel,而不会将其大小限制为JFrame的最大大小。它还具有无限分辨率外观,可以直接打印组件所获得的字体和内容,而不需要像trashgod建议的那样进行双缓冲。

so that I can validate the JPanel without it being size-limited to the maximum size of a JFrame. It also has the "unlimited resolution" look on the fonts and things that you get from printing the components directly, without double-buffering like trashgod suggested.

这篇关于打印大型Swing组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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