該應用程序用於從數據庫中填寫表單或從該表單寫入數據庫。現在我可以使用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一定是失望了? –