该应用程序用于提取信息以从数据库填写表单或从此表单写入数据库。现在我可以使用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);
}
});
}
}
答案 0 :(得分:3)
来自JTable
API:“J2SE 5将方法添加到JTable
,以便方便地访问一些常见的打印需求。简单的新print()
方法可以快速轻松地添加打印支持对你的申请。“这些方法会打印TableModel
中的所有行,而不仅仅是那些可见的行。
附录:您可以准确打印屏幕上显示的内容,如Printing the Contents of a User Interface所示,或者您可以打印TableModel
的全部内容,如here所示和{{3} }}
附录:这是一个打印JPanel
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);
}
});
}
}