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

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

问题描述

我有一个在 JScrollPane 中带有自定义表的 Swing 表单(它只是一个 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);

这些似乎没什么区别:

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

这会使面板变大,但它停在大约一页半大(1100 像素)处:

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天全站免登陆