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

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

问题描述

我一直在一个大程序中工作,其中一个功能应该是打印主窗口的内容。我检查了API,发现这个例子:

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



  PrinterJob job =在我的程序中使用该代码, PrinterJob.getPrinterJob(); 
job.setPrintable(this);
boolean ok = job.printDialog();
if(ok){
try {

job.print();
} catch(PrinterException ex){
/ *作业未成功完成* /
}
}

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

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

我希望能帮助我成功实现此代码。我知道我应该可以通过检查文档(我已经尝试了将近2天)来完成它,但是我无法完成它的工作。我通过互联网了解了我所知道的所有编程。任何帮助将不胜感激。

解决方案

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

  public static class Printer implements {
最终组件comp;

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

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

//获取组件边界
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();

//获取可打印区域的边界
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);

返回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(preformat!= postformat){
//设置打印组件
pjob.setPrintable(new Printer(yourComponent),postformat);
if(pjob.printDialog()){
pjob.print();
}
}


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

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 */
         }
     }

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?

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;
    }
}

Next, to print it:

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