java.awt.print 只打印第一页

发布于 2024-12-16 23:12:49 字数 2030 浏览 3 评论 0原文

我试图根据 Oracle 网页中的打印示例打印一个列表,由于某种原因,它只会打印列表的第一页,

我从 Oracle 获取了有关基本打印的打印示例: 基本打印程序

并在此处修改它以解释我的意思更好一点的是,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class HelloWorldPrinter implements Printable, ActionListener {


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

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        g.drawString("1.Hello world!", 100, 100);
        g.drawString("2.Hello world!", 100, 300);
        g.drawString("3.Hello world!", 100, 600);
        g.drawString("4.Hello world!", 100, 800);
        g.drawString("5.Hello world!", 100, 1000);
        g.drawString("6.Hello world!", 100, 1200);
        g.drawString("7.Hello world!", 100, 1500);

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
             }
         }
    }

    public static void main(String args[]) {

        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Hello World Printer");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JButton printButton = new JButton("Print Hello World");
        printButton.addActionListener(new HelloWorldPrinter());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
}

这个例子只会打印前 4 行......

i m trying to print a list according to the print example in oracle's web page and for some reason it would only print the first page of the list

i took the printing sample from oracle about basic printing :
A Basic Printing Program

and modified it here to explain what i mean a little bit better

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class HelloWorldPrinter implements Printable, ActionListener {


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

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        g.drawString("1.Hello world!", 100, 100);
        g.drawString("2.Hello world!", 100, 300);
        g.drawString("3.Hello world!", 100, 600);
        g.drawString("4.Hello world!", 100, 800);
        g.drawString("5.Hello world!", 100, 1000);
        g.drawString("6.Hello world!", 100, 1200);
        g.drawString("7.Hello world!", 100, 1500);

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
             }
         }
    }

    public static void main(String args[]) {

        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Hello World Printer");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JButton printButton = new JButton("Print Hello World");
        printButton.addActionListener(new HelloWorldPrinter());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
}

this example would only print the first 4 lines ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-12-23 23:12:49

我有一段时间也遇到类似的问题。 这个示例帮了我很多忙正确分页。正如我的问题中所建议的,此处,尝试在教程示例中复制并确保它有效,然后开始一次添加一个部分,直到它停止工作,这样您就可以看到问题出在哪里。

编辑:哎呀,没有注意到答案来自OP。哦,好吧,如果有人遇到这个问题并且OP的答案不起作用,也许我的答案会起作用。

I had a similar problem for quite a while. This example helped me out a lot in getting it paginating properly. As was suggested in my question, here, try copying in the tutorial example and make sure it works, then start adding your stuff one section at a time until it stops working, so you can see where the problem is.

Edit: Oops, didn't notice that the answer was from the OP. Oh, well, if anyone runs across this and OP's answer doesn't work, maybe mine will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文