Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javagui的打印_Java_User Interface_Printing - Fatal编程技术网

javagui的打印

javagui的打印,java,user-interface,printing,Java,User Interface,Printing,我已经用Java创建了一个GUI,它有一个按钮名Print…在打印GUI时,它可以正确打印,但打印需要很长时间…请帮助我如何减少等待时间…谢谢 我正在使用以下代码进行打印 public void actionPerformed(ActionEvent ae) { if (ae.getSource() == bprint) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(

我已经用Java创建了一个GUI,它有一个按钮名Print…在打印GUI时,它可以正确打印,但打印需要很长时间…请帮助我如何减少等待时间…谢谢

我正在使用以下代码进行打印

public void actionPerformed(ActionEvent ae)
{
    if (ae.getSource() == bprint)
    {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        System.out.println("here");
        if (ok)
        {
            try
            {
                job.print();
            }
            catch (PrinterException ex)
            {
                System.out.println(ex);
            }
        }
    }

}

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

    p1.printAll(g);

    return PAGE_EXISTS;
}

打印要花很长时间。通过在单独的线程中执行打印,可以减少运行时间

您必须将JFrame(打印整个GUI)或希望打印的Swing组件传递到打印线程

    printButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            new Thread(new PrintActionListener(frame)).start();
        }
    });
这是我的打印机线程

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JPanel;

import com.ggl.sudoku.solver.view.SudokuFrame;

public class PrintActionListener implements Runnable {

    private SudokuFrame frame;

    public PrintActionListener(SudokuFrame frame) {
        this.frame = frame;
    }

    @Override
    public void run() {
        final BufferedImage image = createPanelImage();

        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(new ImagePrintable(printJob, image));

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (PrinterException prt) {
                prt.printStackTrace();
            }
        }
    }

    private BufferedImage createPanelImage() {
        JPanel panel = frame.getSudokuPanel();
        BufferedImage image = new BufferedImage(panel.getWidth(),
                panel.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        panel.paint(g);
        g.dispose();
        return image;
    }

    public class ImagePrintable implements Printable {

        private double          x, y, width;

        private int             orientation;

        private BufferedImage   image;

        public ImagePrintable(PrinterJob printJob, BufferedImage image) {
            PageFormat pageFormat = printJob.defaultPage();
            this.x = pageFormat.getImageableX();
            this.y = pageFormat.getImageableY();
            this.width = pageFormat.getImageableWidth();
            this.orientation = pageFormat.getOrientation();
            this.image = image;
        }

        @Override
        public int print(Graphics g, PageFormat pageFormat, int pageIndex)
                throws PrinterException {
            if (pageIndex == 0) {
                int pWidth = 0;
                int pHeight = 0;
                if (orientation == PageFormat.PORTRAIT) {
                    pWidth = (int) Math.min(width, (double) image.getWidth());
                    pHeight = pWidth * image.getHeight() / image.getWidth();
                } else {
                    pHeight = (int) Math.min(width, (double) image.getHeight());
                    pWidth = pHeight * image.getWidth() / image.getHeight();
                }
                g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }

    }

}