该应用程序用于从数据库中填写表单或从该表单写入数据库。现在我可以使用netbeans来做这两件事,并联系我现在已经运行的MySQL测试服务器。将JFrame信息发送给打印机
我遇到的问题是我需要以类似于表格的方式打印从数据库获取的信息,以便匹配我们目前在办公室使用的手写表单。有没有办法打印整个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? – Friendlyghost89
呃......我想Google一定是失望了? –