浏览 5677 次
锁定老帖子 主题:关于JTable的打印讨论
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-11-10
最后修改:2009-11-10
这些是在界面上的显示,但是如果要打印出来呢? 下面就以打印为住,讨论几种实现方式: 1、JTable的print(),这是直接调用JTable的打印方法,打印是直接将界面的样式打印出去的,例如这个Demo: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.text.*; public class TablePrint { public static void main(String args[]) { final Object rows[][] = { {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, {"one", "ichi - \u4E00"}, {"two", "ni - \u4E8C"}, {"three", "san - \u4E09"}, {"four", "shi - \u56DB"}, {"five", "go - \u4E94"}, {"six", "roku - \u516D"}, {"seven", "shichi - \u4E03"}, {"eight", "hachi - \u516B"}, {"nine", "kyu - \u4E5D"}, {"ten", "ju - \u5341"}, }; final Object headers[] = {"English", "Japanese"}; Runnable runner = new Runnable() { public void run() { JFrame frame = new JFrame("Table Printing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Print"); ActionListener printAction = new ActionListener() { public void actionPerformed(ActionEvent e) { try { MessageFormat headerFormat = new MessageFormat("Page {0}"); MessageFormat footerFormat = new MessageFormat("- {0} -"); table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat); } catch (PrinterException pe) { System.err.println("Error printing: " + pe.getMessage()); } } }; button.addActionListener(printAction); frame.add(button, BorderLayout.SOUTH); frame.setSize(300, 150); frame.setVisible(true); } }; EventQueue.invokeLater(runner); } } 应该说比较简单,但是正式报表几乎没法用。 2.通过JasperReport工具来实现打印 目前大部分报表打印应该都是通过JasperReport来实现的,不知道的可以看看之前写的一个教程:JasperReport 个人使用的一些经验 问题是如果表头固定,那么模板还好设计,如果表头不固定呢?那就要自己来通过JasperDesign来绘制了,稍微有点麻烦了,这块自己的处理方式是表头固定的JasperReport,不固定的就直接Swing绘制,就是下面要讲的一种。 3. 通过 print(Graphics g, PageFormat pf, int pageIndex)方法手动绘制 这个方法的好处是基本上所有报表应该都不是问题,关键是绘制起来有些麻烦了,需要手动考虑分页问题,以及字体设置,绘制表头,绘制数据区域,绘制表尾等。 上面三个方法是目前我知道的,第一种几乎就没法在报表中使用,一般是第二种方法,第三种方法是实在用第二种不好实现时使用的。 不知道大伙项目中遇上复杂的报表打印是如何处理的呢? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-11-10
生成PDF,然后打开PDF阅读器,然后用户爱怎么打印怎么打印。
|
|
返回顶楼 | |
发表时间:2009-11-11
carlkkx 写道 生成PDF,然后打开PDF阅读器,然后用户爱怎么打印怎么打印。
关键问题是有些时候,用户只要打印,即使我导出成Excel也不成,按钮下去,打印机出来,没有多余步骤! |
|
返回顶楼 | |
发表时间:2009-11-13
Disney2002 写道 carlkkx 写道 生成PDF,然后打开PDF阅读器,然后用户爱怎么打印怎么打印。
关键问题是有些时候,用户只要打印,即使我导出成Excel也不成,按钮下去,打印机出来,没有多余步骤! 一般用户都需要打印预览吧?这不就借用PDF阅读器作为打印预览,然后用PDF阅读器的打印命令打印,想必一般用户都能接受吧。 |
|
返回顶楼 | |