简体   繁体   English

将参数传递给 Java 中的打印函数的问题

[英]Issues with passing arguments to the print function in Java

public int print(Graphics g, PageFormat pf, int page, String customer_name) 
    throws PrinterException {

    System.out.println("The value of customer name:"+customer_name);

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

    String x = layout.get("");
    System.out.println("The value of x is\n"+x);
    /* Now we perform our rendering */
    g.drawString("Customer Name: "+customer_names, 100, 100);


    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

I want to call this method from another class, while passing an additional argument to it, customer_name .我想从另一个类调用这个方法,同时向它传递一个额外的参数customer_name I call this method from another class as follow:我从另一个类调用这个方法如下:

Printer print = new Printer();  //making an object to access that class Printer.java
PageFormat page = job.defaultPage();
print.print(<I have no idea what to put here for graphics>, page, 5, customer_name_field.getText());

When I call the method print.print , I gives the message that it requires Graphics, PageFormat, int, String .当我调用方法print.print ,我给出了它需要Graphics, PageFormat, int, String But what should I put for Graphics , I have no idea?但是我应该为Graphics放什么,我不知道?

It's not working because it looks like you're going about it wrong:它不起作用,因为看起来你做错了:

  • Your print method has an extra String parameter tacked to the end which prevents it from being a true method override for a class that implements Printable.您的打印方法在末尾附加了一个额外的字符串参数,以防止它成为实现 Printable 的类的真正方法覆盖。
  • You're trying to call your print method directly scrounging around for a Graphics context when you shouldn't consider doing this.当您不应该考虑这样做时,您正试图直接调用您的打印方法来寻找 Graphics 上下文。

If you just want to print some text then you need to follow the first sections of the Printing Tutorial .如果您只想打印一些文本,那么您需要遵循 打印教程的第一部分。 Your print method above does not conform with a Printable's print(...) method override.您上面的打印方法不符合 Printable 的print(...)方法覆盖。 Please do yourself a favor and follow the tutorial.请帮自己一个忙并按照教程进行操作。 I've given you the link.我已经给你链接了。

Consider creating a class that implements Printable, passing your String as a single parameter to the class's constructor, and use this to set an instance field.考虑创建一个实现 Printable 的类,将 String 作为单个参数传递给类的构造函数,并使用它来设置实例字段。 The print(...) method should match that found in the tutorial, should have an @Override annotation, and most important will never be called directly by you . print(...)方法应该与教程中找到的方法相匹配,应该有一个@Override注释,最重要的是永远不会被你直接调用 Your PrinterJob instance will do the printing behind the scenes.您的 PrinterJob 实例将在幕后进行打印。

Note, that if your goal is to print a Swing GUI, then the steps are different, since Swing GUI's carry much of the innate machinery for printing within them.请注意,如果您的目标是打印 Swing GUI,那么步骤是不同的,因为 Swing GUI 带有很多用于打印的固有机制。

Depends on the class you're in, there's a high chance that the UI framework element you are currently using already provides you with the getGraphics() function.取决于您所在的课程,您当前使用的 UI 框架元素很有可能已经为您提供了 getGraphics() 函数。

You might even want to override the paint() function of the class you're using, and call this method you have written from that.您甚至可能想要覆盖您正在使用的类的paint() 函数,并调用您从中编写的方法。 It depends on your use-case.这取决于您的用例。

This is my github project path where i did the code for same.这是我的 github 项目路径,我在这里做了相同的代码。 https://github.com/knikam/Mobile_shop_management/blob/master/src/mobile_shop_mangment/Sell_mobile.java https://github.com/knikam/Mobile_shop_management/blob/master/src/mobile_shop_mangment/Sell_mobile.java

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

    String x = layout.get("");
    System.out.println("The value of x is\n"+x);
    /* Now we perform our rendering */
    String customer_namescustomer_names.getText();**This is work for me**
    g.drawString("Customer Name: "+customer_names, 100, 100);


    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

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

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