[英]Print JFrame in Java Application (Using Netbeans)
我正在尝试打印JFrame的内容,但是通过查看不同的教程,我仍然没有运气。目前,用于打印的代码部分是
public class View_Check extends javax.swing.JFrame implements Printable{
JFrame frameToPrint;
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
frameToPrint.printAll(g);
return PAGE_EXISTS;
}
并单击按钮(jButton2)
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
}
我肯定会丢失一些东西,但是尝试向View_Check添加“ JFrame f”并没有帮助...和新的(View_Check(f)),因为找不到f蚂蚁NetBeans正在变成带有红色的圣诞树线
以防万一这是一个
public View_Check( Model_Customer cust) {
initComponents();
lblinfo1.setVisible(false);
customer = cust;
lblname.setText(customer.GetFName());
lblsurname.setText(customer.GetLName());
lblMake.setText(customer.GetMake());
lblModel.setText(customer.GetModel());
lblEngine.setText(customer.GetEngine());
lblRegistration.setText(customer.GetRegistration());
lblMileage.setText(customer.GetMileage());
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
lblDate.setText(date);
lblPostcode.setText(customer.GetPostcode());
lblNumber.setText(customer.GetNumber());
}
更改了frameToPrint.printAll(g); 到this.printAll(g); 并像魅力一样工作:)谢谢您的帮助
尝试将JFrame frameToPrint设置为静态,看看是否可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.