【问题标题】:Print jLabel's icon in a printer using a button [closed]使用按钮在打印机中打印 jLabel 的图标 [关闭]
【发布时间】:2014-02-27 03:58:22
【问题描述】:

我有一个带有图标的 jLabel,我想使用一个按钮在打印机(canon、hp、epson 任何东西)中打印它。 我怎样才能做到这一点? 有什么有用的代码吗? 代码sn-p? 链接? 我能看到的都是这样的: How to print content of a label in java?

但这不是我想要的。 我正在使用netbeans 提前致谢。

【问题讨论】:

  • 看看this example
  • 它正在打印一个 Jframe。我只想打印 jLabel。
  • 如果您花时间查看代码而不是标题,您会看到这个名为 public static void printComponent(JComponent comp, boolean fill) 的好方法 ...纸....打印框架通常与在 Swing 中打印几乎任何组件的过程相同...这就是它的优点

标签: java netbeans printing jbutton jlabel


【解决方案1】:

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

这可以使用setSize 并在最基本的级别上使用getPreferredSize 来完成。

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

以首选尺寸打印的示例...

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

现在示例使用printComponentToFile 方法,但您需要使用printComponent 方法来实际打印打印机,第一个方法对于执行页面预览和屏幕转储等操作很有用...

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

    }

}

【讨论】:

  • 这很好,但我可以有一个更短的吗?或者可以放在按钮下
  • 只有当你付钱给我时——即不,这大约是你能得到它的时间,除非你不费心显示打印对话框,但是你怎么知道它被打印在哪里......
  • 我想知道我应该在按钮下面放什么。
  • 我无法将它放到 netbeans 上。不知道我应该放什么
  • 这是我的代码:pastebin.com/FYqi4uks 不幸的是,我现在无法在此处发布任何内容:(
猜你喜欢
  • 1970-01-01
  • 2012-07-12
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2021-12-11
相关资源
最近更新 更多