java打印

 try {
                // fetch the printable
                Printable printable = currentPerformTable.getPrintable(JTable.
                        PrintMode.FIT_WIDTH,
                        new MessageFormat("打印"),
                        new MessageFormat("Page - {0}"));
                // fetch a PrinterJob
                PrinterJob job = PrinterJob.getPrinterJob();
                // set the Printable on the PrinterJob
                job.setPrintable(printable);
                // create an attribute set to store attributes from the print dialog
                PrintRequestAttributeSet attr = new
                                                HashPrintRequestAttributeSet();
                // display a print dialog and record whether or not the user cancels it
                boolean printAccepted = job.printDialog(attr);
                // if the user didn't cancel the dialog
                if (printAccepted) {
                    // do the printing (may need to handle PrinterException)
                    try {
                        job.print(attr);
                    } catch (PrinterException ex) {
                        System.err.print(ex);
                    }
                }
            } finally {
                // restore the original table state here (for example, restore selection)
            }

 

 try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(chartPanel); //打印图表
                // create an attribute set to store attributes from the print dialog
                PrintRequestAttributeSet attr = new
                                                HashPrintRequestAttributeSet();
                // display a print dialog and record whether or not the user cancels it
                boolean printAccepted = job.printDialog(attr);
                // if the user didn't cancel the dialog
                if (printAccepted) {
                    // do the printing (may need to handle PrinterException)
                    try {
                        job.print(attr);
                    } catch (PrinterException ex) {
                        System.err.print(ex);
                    }
                }
            } finally {
                // restore the original table state here (for example, restore selection)
            }

 

你可能感兴趣的:(java)