且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

打印JFrame及其组件

更新时间:2023-12-05 22:06:52

这是一个简单的解决方案。我有一个实现打印的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();
    }
}