简体   繁体   English

在Java应用程序中打印JFrame(使用Netbeans)

[英]Print JFrame in Java Application (Using Netbeans)

I'm trying to print the content of JFrame but, by looking at different tutorials, I still din't have any luck... Currently Parts of code for printing are 我正在尝试打印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;
    }

and on the click of the button (jButton2) 并单击按钮(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) {

             }
         }

    }            

Im Sure something is missing, but by trying to add "JFrame f" to View_Check didn't help... and new (View_Check(f)) either as f couldn't be found ant NetBeans was turning into a christmas tree with red lines 我肯定会丢失一些东西,但是尝试向View_Check添加“ JFrame f”并没有帮助...和新的(View_Check(f)),因为找不到f蚂蚁NetBeans正在变成带有红色的圣诞树线

just in case here is a 以防万一这是一个

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());               
    }

Changed frameToPrint.printAll(g); 更改了frameToPrint.printAll(g); to this.printAll(g); 到this.printAll(g); and worked Like a charm :) Thanks for your help 并像魅力一样工作:)谢谢您的帮助

尝试将JFrame frameToPrint设置为静态,看看是否可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM