【发布时间】: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 吗?
-
嗯...我猜谷歌一定是倒闭了?
-
我不知道您过去是否遇到过任何好的并且您会推荐的具体示例。当然我已经在查找它们了,但是如果有人可以展示好的例子,那么为什么要重新找到那些好的例子呢?