打印 JFrame 及其组件 [英] Printing a JFrame and its components

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

问题描述

我一直在开发一个大程序,它的功能之一应该是打印主窗口的内容.我检查了 API 并找到了这个例子:

I have been working in a big program and one of its functionalities should be to print the contents of the main window. I checked the API and found this example:

http://docs.oracle.com/javase/tutorial/2d/printing/gui.html

这非常有帮助,我尝试在我的程序中使用该代码,方法是将它放在我的打印按钮的 actionperformed 方法中:

it was very helpful, I tried to use that code in my program by placing this inside the actionperformed method of my print button:

PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable(this);
     boolean ok = job.printDialog();
     if (ok) {
         try {

             job.print();
         } catch (PrinterException ex) {
          /* The job did not successfully complete */
         }
     }

如果我点击打印按钮,我会看到一个打印机对话框,当我告诉它打印时,它只会打印一个空白文档.我知道上面的代码并不是我所需要的,正如我在 API 的示例中看到的那样,有一个 print() 方法,但显然他们从未调用过它,所以这很令人困惑.我试过多次调用和使用它,但没有成功.

If I click the print button, I get a printer dialog and when I tell it to print, it just prints a blank document. I know the above code is not all I need, as I've seen in the API's examples there is a print() method, but apparently they never call it, so it is pretty confusing. I've tried calling and using it many times, but with no success.

另外,我认为当我最终打印它时,我的窗口需要以横向打印,甚至可能需要一些缩放.关于如何做到这一点的任何想法?

Also, I think that when I finally get it to print, my window will need to be printed in the landscape orientation, it even may need some scaling. Any ideas on how to do that?

我想要任何有用的帮助来帮助我成功实现此代码.我知道我应该能够通过查看文档(我已经尝试了近 2 天)自己完成,但我无法让它工作.我已经通过互联网学习了我所知道的所有编程.任何帮助将不胜感激.

I would like any useful help to help me implement this code successfully. I know I should be able to do it by myself just by checking the documentation (I've tried for almost 2 days now) but I can't get it to work. I've learned all the programming I know through the internet. Any help will be greatly appreciated.

推荐答案

这是一个简单的解决方案.我有一个实现可打印的打印机类,它将处理打印作业:

Here's a simple solution. I have a Printer class that implements printable and it will handle the printing job:

public static class Printer implements Printable {
    final Component comp;

    public Printer(Component comp){
        this.comp = comp;
    }

    @Override
    public int print(Graphics g, PageFormat format, int page_index) 
            throws PrinterException {
        if (page_index > 0) {
            return Printable.NO_SUCH_PAGE;
        }

        // get the bounds of the component
        Dimension dim = comp.getSize();
        double cHeight = dim.getHeight();
        double cWidth = dim.getWidth();

        // get the bounds of the printable area
        double pHeight = format.getImageableHeight();
        double pWidth = format.getImageableWidth();

        double pXStart = format.getImageableX();
        double pYStart = format.getImageableY();

        double xRatio = pWidth / cWidth;
        double yRatio = pHeight / cHeight;


        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pXStart, pYStart);
        g2.scale(xRatio, yRatio);
        comp.paint(g2);

        return Printable.PAGE_EXISTS;
    }
}

接下来,打印它:

JFrame yourComponent = new JFrame();
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat preformat = pjob.defaultPage();
preformat.setOrientation(PageFormat.LANDSCAPE);
PageFormat postformat = pjob.pageDialog(preformat);
//If user does not hit cancel then print.
if (preformat != postformat) {
    //Set print component
    pjob.setPrintable(new Printer(yourComponent), postformat);
    if (pjob.printDialog()) {
        pjob.print();
    }
}

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

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