打印时JPanel的内容仅显示在页面顶部 [英] Contents of JPanel only shown at the top of the page while printing

查看:45
本文介绍了打印时JPanel的内容仅显示在页面顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试向我使用 Java 8 创建的字符生成器添加打印功能.我的问题是,使用显示的代码时,每个元素只会显示在页面顶部.

完整代码可以在 github 上找到.打印部分位于 src/main/java/antafes/vampireEditor/gui/BaseWindow.java 和页面对象可以在 src/main/java/antafes/vampireEditor/print/.

对我来说另一个奇怪的事情是,lambda 函数中的部分被调用了大约 10 次......这正常吗?

如果您需要更多信息,请告诉我.

如果我使用已为字符显示的打印预览元素,至少对于当前选定的页面(注释掉的三行),我已设法使其正常工作.

if (!this.isAnyCharacterLoaded()) {返回;}PrinterJob printerJob = PrinterJob.getPrinterJob();printerJob.setJobName(this.language.translate("printCharacter"));printerJob.setPrintable ((graphics, pageFormat, pageNum) -> {如果(pageNum > 0){返回 Printable.NO_SUCH_PAGE;}Graphics2D graphics2D = (Graphics2D) 图形;graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());//CharacterTabbedPane pane = (CharacterTabbedPane) charactersTabPane.getSelectedComponent();//PrintPreviewPanel printPreview = pane.getPrintPreview();//printPreview.printContent(graphics2D);打印基页;开关(pageNum){案例0:默认:page = new General(this.getActiveCharacter(), false);休息;}维度维度 = new Dimension((int) pageFormat.getWidth(), (int) pageFormat.getHeight());page.setSize(dimension);页面创建();page.paint(graphics2D);返回 Printable.PAGE_EXISTS;});如果 (!printerJob.printDialog()) {返回;}尝试 {打印机作业.print();} catch (PrinterException ex) {//处理异常}

页面上的内容应该像打印预览一样显示.

解决方案

问题是组件布局不正确.@second 为我指明了正确的方向.正如他建议的那样,我查看了 ScreenImage 类.使用该类创建图像有效,因此我开始使用 ScreenImage 类实现打印过程.

经过一段时间和一些关于该过程的想法后,我认为我只需要这个类的一个方法. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .我采用了 layoutComponent() 方法并只使用了它.使用此功能后,元素布局正确,现在以我想要的方式显示.

private void layoutComponent(组件组件){同步 (component.getTreeLock()){组件.doLayout();if(容器的组件实例){for (Component child : ((Container)component).getComponents()){this.layoutComponent(child);}}}}

稍后对论文进行了一些调整,一切看起来都应该如此:)

再次感谢@second 为我指明了正确的方向.

I'm currently trying to add print functionality to a character generator I'm creating using Java 8. My problem is, that every single element will only be shown at the top of the page while using the shown code.

The whole code can be found on github. The print part is located under src/main/java/antafes/vampireEditor/gui/BaseWindow.java and the page objects can be found in src/main/java/antafes/vampireEditor/print/.

Another weird thing to me is, that the part in the lambda function is called around 10 times... Is that normal?

If you need further information, let me know.

I've managed to get it working, if I use the print preview element that is already shown for a character, at least for the currently selected page (the three lines commented out).

if (!this.isAnyCharacterLoaded()) {
    return;
}

PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setJobName(this.language.translate("printCharacter"));
printerJob.setPrintable ((graphics, pageFormat, pageNum) -> {
    if (pageNum > 0){
        return Printable.NO_SUCH_PAGE;
    }

    Graphics2D graphics2D = (Graphics2D) graphics;
    graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    //CharacterTabbedPane pane = (CharacterTabbedPane) charactersTabPane.getSelectedComponent();
    //PrintPreviewPanel printPreview = pane.getPrintPreview();
    //printPreview.printContent(graphics2D);
    PrintBase page;

    switch (pageNum) {
        case 0:
        default:
            page = new General(this.getActiveCharacter(), false);
            break;
    }

    Dimension dimension = new Dimension((int) pageFormat.getWidth(), (int) pageFormat.getHeight());
    page.setSize(dimension);
    page.create();
    page.paint(graphics2D);

    return Printable.PAGE_EXISTS;
});

if (!printerJob.printDialog()) {
    return;
}

try {
    printerJob.print();
} catch (PrinterException ex) {
    // handle exception
}

The contents on the page should be displayed like on the print preview.

解决方案

The problem was, that the component wasn't layouted correctly. @second pointed me in the right direction. As he suggested I took a look into the ScreenImage class. Creating an image with this class worked and thus I started implementing the print process using the ScreenImage class.

After a while and some thoughts about the process I figured I only need exactly one method of this class. I took the layoutComponent() method and used only that. With this in use the elements were layouted correctly and now show up the way I want them to.

private void layoutComponent(Component component)
{
    synchronized (component.getTreeLock())
    {
        component.doLayout();

        if (component instanceof Container)
        {
            for (Component child : ((Container)component).getComponents())
            {
                this.layoutComponent(child);
            }
        }
    }
}

Some adjustments to the paper later everything looks like it should :)

Thanks again to @second for pointing me into the right direction.

这篇关于打印时JPanel的内容仅显示在页面顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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