Java 怎么打印组件

2019年8月18日21:59:30 评论 22

1.简介

Java 怎么打印组件?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;

public class Main{
    private final Component componentToBePrinted;
    private final Printable printable = new Printable() {
        
         
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
            final int response;

            final Graphics2D g2d = (Graphics2D) g;

            final double compHeightPx = componentToBePrinted.getHeight();
            final double compWidthPx = componentToBePrinted.getWidth();
            final double pageWidthPt = pageFormat.getImageableWidth(); 
            final double pageHeightPt = pageFormat.getImageableHeight(); 

            if (scaleToFit) {
                
                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                }
                final double scaleX = pageWidthPt / compWidthPx;
                final double scaleY = pageHeightPt / compHeightPx;
                
                final double scale = Math.min(scaleX, scaleY);
                g2d.scale(scale, scale);

                
                g2d.translate(pageFormat.getImageableX(),
                        pageFormat.getImageableY());

                componentToBePrinted.printAll(g2d);

                return PAGE_EXISTS;
            }

            final double scale = pageWidthPt / compWidthPx;
            final int totalNumPages = (int) Math.ceil(scale * compHeightPx
                    / pageHeightPt);
            
            if (pageIndex >= totalNumPages) {
                response = NO_SUCH_PAGE;
            } else {
                
                g2d.translate(pageFormat.getImageableX(),
                        pageFormat.getImageableY());
                
                g2d.translate(0f, -pageIndex * pageHeightPt);
                
                g2d.scale(scale, scale);

                componentToBePrinted.printAll(g2d);

                response = Printable.PAGE_EXISTS;
            }
            return response;
        }
    };
    public static void printComponent(Component c, boolean scaleToFit) {
        new PrintComponentUtil(c, scaleToFit).print((PageFormat) null);
    }
    public static void printComponent(Component c, boolean scaleToFit,
            PageFormat pageFormat) {
        new PrintComponentUtil(c, scaleToFit).print(pageFormat);
    }
    public void print(PageFormat pageFormat) {
        final PrinterJob printJob = PrinterJob.getPrinterJob();
        if (pageFormat != null) {
            printJob.setPrintable(printable, pageFormat);
        } else {
            printJob.setPrintable(printable);
        }
        if (printJob.printDialog()) {
            
            

            final JOptionPane optionPane = new JOptionPane("Printing...",
                    JOptionPane.PLAIN_MESSAGE);
            optionPane.setOptions(new Object[] {}); 
            final JDialog dialog = new JDialog(
                    SwingLib.findFrameParent(componentToBePrinted),
                    "Printing", true);
            dialog.setContentPane(optionPane);
            dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            dialog.setLocationRelativeTo(SwingLib
                    .findFrameParent(componentToBePrinted));

            new Thread("Peralex - Print Component") {
                @Override
                public void run() {
                    try {
                        printJob.print();
                    } catch (PrinterException ex) {
                        dialog.setVisible(false);
                        ex.printStackTrace();
                        return;
                    }
                    dialog.setVisible(false);
                }
            }.start();

            dialog.pack();
            dialog.setVisible(true);
        }
    }
}

 

3.总结

示例代码演示了 Java 怎么打印组件,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: