【问题标题】:Sending the JFrame information to the Printer将 JFrame 信息发送到打印机
【发布时间】:2011-11-19 05:52:30
【问题描述】:

该应用程序用于提取信息以从数据库中填写表单或从该表单写入数据库。现在,我可以通过使用 netbeans 并联系我目前已启动并正在运行的 MySQL 测试服务器来完成这两项工作。

我遇到的问题是我需要以类似形式而不是表格的方式打印从数据库获得的信息,以匹配我们目前在办公室使用的手写表格。有没有办法打印整个 JFrame 或 JFrame 中的所有内容,就像它们在屏幕上的布局一样供用户查看?

到目前为止,我看到的所有内容都将打印屏幕的某个区域(文本框)或通过表格打印。

一切就绪后,将为 Linux 和 Windows 编译该应用程序。

代码:

package Information;

import java.awt.print.*;
import java.awt.*;
import javax.swing.*;

public class HATDB extends javax.swing.JFrame implements Printable {

    JFrame frameToPrint;

    /** Creates new form HATDB */
    public HATDB() {
    }

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws
        PrinterException {

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Now print the window and its visible contents */
        frameToPrint.printAll(g);

        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }

    public HATDB(JFrame f) {
        frameToPrint = f;
    }

    private void OK_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
        PrinterJob job = PrinterJob.getPrinterJob();
        //job.setPrintable();
        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                ex.printStackTrace(System.err);
            }
        }

    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */

        JFrame f = new JFrame("Print UI Example");
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HATDB().setVisible(true);
            }
        });
    }
}

【问题讨论】:

  • 是的,您可以像打印其他 Java 组件一样打印 JFrame,但它没有针对创建漂亮的打印输出进行优化。为什么不直接使用 JasperReports 之类的报告工具,因为创建此类程序就是为了解决此类问题?
  • 有什么例子或方向可以告诉我使用 JasperReports 吗?
  • 嗯...我猜谷歌一定是倒闭了?
  • 我不知道您过去是否遇到过任何好的并且您会推荐的具体示例。当然我已经在查找它们了,但是如果有人可以展示好的例子,那么为什么要重新找到那些好的例子呢?

标签: java swing printing


【解决方案1】:

来自JTable API:“J2SE 5 将方法添加到 JTable 以方便访问一些常见的打印需求。简单的新 print() 方法允许快速轻松地为您的应用程序添加打印支持。”这些方法打印您的TableModel 中的所有行,而不仅仅是那些可见的行。

附录:您可以准确打印屏幕上的内容,如Printing the Contents of a User Interface 所示,也可以打印TableModel 的全部内容,如hereChapter 6 Continued: Advanced Printing 所示。

附录:这是一个打印 JPanelJTree 的示例。

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/8192204 */
public class PrintTest extends JPanel implements Printable {

    public PrintTest() {
        this.setLayout(new GridLayout());
        JTree tree = new JTree();
        this.add(new JScrollPane(tree));
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }

    @Override
    public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
        if (i > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        PrintTest.this.printAll(g);
        return Printable.PAGE_EXISTS;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final PrintTest pt = new PrintTest();
                f.add(pt, BorderLayout.CENTER);
                JButton b = new JButton(new AbstractAction("Print") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        PrinterJob pj = PrinterJob.getPrinterJob();
                        PageFormat pf = pj.pageDialog(pj.defaultPage());
                        pj.setPrintable(pt, pf);
                        if (pj.printDialog()) {
                            try {
                                pj.print();
                            } catch (PrinterException pe) {
                                pe.printStackTrace(System.err);
                            }
                        }
                    }
                });
                JPanel p = new JPanel();
                p.add(b);
                f.add(p, BorderLayout.SOUTH);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

【讨论】:

  • 我可以进行简单的打印(打印文本框或表格的内容,但不能打印整个表单)。我遇到的问题是我正在尝试打印屏幕上看到的表格。那是行不通的。
  • 我已经为上述两种方法添加了链接。
  • 我已经添加了上面解释的代码,但我不确定它是否正确(我对 java 有点陌生......)我不知道如何发布文件在这里,我上传了一个简单的程序模型。
  • .form (megaupload.com/?d=41V55V2V) 这些是来自 netbeans 应用程序的 2 个文件......
  • 这可能听起来很愚蠢,但是您想要 sscce 什么?或者我如何从我从 netbeans 获得的代码生成它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多