簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM