使用按钮在打印机中打印 jLabel 的图标 [英] Print jLabel's icon in a printer using a button

查看:23
本文介绍了使用按钮在打印机中打印 jLabel 的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有图标的 jLabel,我想使用按钮在打印机(佳能、惠普、爱普生)中打印该图标.我怎样才能做到这一点?任何有用的代码?代码片段?链接?我只能看到是这样的:如何在java中打印标签的内容?

I have a jLabel with an Icon that I want to print in a printer (canon,hp,epson anything) using a button. How can I do that? Any useful code? Code snippet? links? All I can see is like this: How to print content of a label in java?

但这不是我想要的.我正在使用网豆提前致谢.

But It's not what I want. I'm using netbeans Thanks in advance.

推荐答案

基本上,答案将取决于标签是否显示在屏幕上.为确保标签(或实际上,任何组件)可以打印,必须首先正确调整尺寸...

Basically, the answer will depend on whether the label is displayed on the screen or not. To ensure that the label (or in fact, any component) can be printed, it must first be sized properly...

这可以通过使用 setSize 并在非常基本的级别提供 getPreferredSize 来完成.

This can be done using setSize and feeding it getPreferredSize at the very basic level.

下一步是使用组件 printAll 方法(或 print 方法,具体取决于您的需要)传递,它更适合...打印...因为它禁用双缓冲,并且在它未连接到本机对等方时不会产生令人讨厌的异常...

The next step is passing using the components printAll method (or print method depending on your needs) which is better suited for...printing...as it disables double buffering and won't produce nasty exceptions when it's not attached to a native peer...

示例打印为首选尺寸...

Example printed as preferred size...

示例打印以填充可用区域...

Example printed to fill available area...

现在这个例子使用了 printComponentToFile 方法,但是你会想要使用 printComponent 方法来实际打印一个打印机,第一个对于做这样的事情很有用页面预览和屏幕转储...

Now the example uses the printComponentToFile method, but you'll want to use the printComponent method for actually printing it a printer, the first is useful for doing things like page previews and screen dumps...

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class PrintALabel {

    public static void main(String[] args) {
        try {
            JLabel label = new JLabel(
                    "This is a test",
                    new ImageIcon("path/to/image"),
                    JLabel.CENTER);
            printComponentToFile(label, true);
            printComponentToFile(label, false);
        } catch (PrinterException exp) {
            exp.printStackTrace();
        }
    }

    public static void printComponent(JComponent comp, boolean fill) throws PrinterException {
        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat pf = pjob.defaultPage();
        pf.setOrientation(PageFormat.LANDSCAPE);

        PageFormat postformat = pjob.pageDialog(pf);
        if (pf != postformat) {
            //Set print component
            pjob.setPrintable(new ComponentPrinter(comp, fill), postformat);
            if (pjob.printDialog()) {
                pjob.print();
            }
        }
    }

    public static void printComponentToFile(Component comp, boolean fill) throws PrinterException {
        Paper paper = new Paper();
        paper.setSize(8.3 * 72, 11.7 * 72);
        paper.setImageableArea(18, 18, 559, 783);

        PageFormat pf = new PageFormat();
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.LANDSCAPE);

        BufferedImage img = new BufferedImage(
                (int) Math.round(pf.getWidth()),
                (int) Math.round(pf.getHeight()),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fill(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
        ComponentPrinter cp = new ComponentPrinter(comp, fill);
        try {
            cp.print(g2d, pf, 0);
        } finally {
            g2d.dispose();
        }

        try {
            ImageIO.write(img, "png", new File("Page-" + (fill ? "Filled" : "") + ".png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static class ComponentPrinter implements Printable {

        private Component comp;
        private boolean fill;

        public ComponentPrinter(Component comp, boolean fill) {
            this.comp = comp;
            this.fill = fill;
        }

        @Override
        public int print(Graphics g, PageFormat format, int page_index) throws PrinterException {

            if (page_index > 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D) g;
            g2.translate(format.getImageableX(), format.getImageableY());

            double width = (int) Math.floor(format.getImageableWidth());
            double height = (int) Math.floor(format.getImageableHeight());

            if (!fill) {

                width = Math.min(width, comp.getPreferredSize().width);
                height = Math.min(height, comp.getPreferredSize().height);

            }

            comp.setBounds(0, 0, (int) Math.floor(width), (int) Math.floor(height));
            if (comp.getParent() == null) {
                comp.addNotify();
            }
            comp.validate();
            comp.doLayout();
            comp.printAll(g2);
            if (comp.getParent() != null) {
                comp.removeNotify();
            }

            return Printable.PAGE_EXISTS;
        }

    }

}

这篇关于使用按钮在打印机中打印 jLabel 的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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