将 JFrame 信息发送到打印机 [英] Sending the JFrame information to the Printer

查看:59
本文介绍了将 JFrame 信息发送到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The application is for pulling information to fill out a form from a database or write to the database from this form. Right now I can do both of those with using netbeans and contacting my MySQL test server that I have up and running at the moment.

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

The issue I am having is I need to print the information attained from the database in a form like manner rather than a table, in order to match the hand written forms we are currently using at the office. Is there a way to print the entire JFrame or all the contents in the JFrame just as they are laid out on the screen for the user to see?

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

Everything that I have seen thus far will print either a region of the screen (text box) or print it via a table.

当一切都完成后,该应用程序将同时为 Linux 和 Windows 编译.

The application will be compiled for both Linux and Windows when all is said and done.

代码:

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

推荐答案

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

From the JTable API: "J2SE 5 adds methods to JTable to provide convenient access to some common printing needs. Simple new print() methods allow for quick and easy addition of printing support to your application." These methods print all the rows in your TableModel, rather than just those rows that are visible.

附录:您可以准确打印屏幕上的内容,如中所示打印用户界面的内容,或者您可以打印TableModel 的全部内容,如这里第 6 章续:高级打印.

Addendum: You can print exactly what's on the screen, as shown in Printing the Contents of a User Interface, or you can print the entire contents of the TableModel, as shown here and in Chapter 6 Continued: Advanced Printing.

附录:这是一个使用 JTree 打印 JPanel 的示例.

Addendum: Here's an example that prints a JPanel with a JTree.

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

这篇关于将 JFrame 信息发送到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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