问题描述
package com.test;import java.awt.Component;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.awt.print.PrinterJob;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.print.attribute.HashAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import javax.print.attribute.standard.Copies;import javax.print.attribute.standard.MediaPrintableArea;import javax.print.attribute.standard.MediaSizeName;import javax.print.attribute.standard.OrientationRequested;import javax.print.attribute.standard.PrinterName;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;public class Te extends JFrame {private PrintRequestAttributeSet attributes;public Te() {try {attributes = new HashPrintRequestAttributeSet();PrinterJob job = PrinterJob.getPrinterJob();attributes.add(new Copies(1)); // 设置打印分数attributes.add(OrientationRequested.PORTRAIT); // 设置打印方向attributes.add(MediaSizeName.ISO_A6); // 设置打印纸装HashAttributeSet hs = new HashAttributeSet();hs.add(new PrinterName("zhushi", null));PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);job.setPrintService(pss[0]);PrintPanel printTable = new PrintPanel(); int hei = printTable.getHeight2();attributes.add(new MediaPrintableArea(0,0,58,hei,MediaPrintableArea.MM));//设置打印纸装大小job.setPrintable(printTable);job.print(attributes);} catch (Exception e) {}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(1212);Te te = new Te();}}class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {public TableCellTextAreaRenderer() {setLineWrap(true);setWrapStyleWord(true);}public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {// 计算当下行的最佳高度int maxPreferredHeight = 0;for (int i = 0; i < table.getColumnCount(); i++) {setText("" + table.getValueAt(row, i));setSize(table.getColumnModel().getColumn(column).getWidth(), 0);maxPreferredHeight = Math.max(maxPreferredHeight,getPreferredSize().height);}if (table.getRowHeight(row) != maxPreferredHeight) // 少了这行则处理器瞎忙table.setRowHeight(row, maxPreferredHeight);setText(value == null ? "" : value.toString());System.err.println(value+" vvvvvvvvvvvvvv");System.err.println(maxPreferredHeight+" sssssssssssssssssss");/*hei += maxPreferredHeight;System.out.println(hei);*/return this;}//public static int hei =0;//public int getHei(JTable table, Object value,//boolean isSelected, boolean hasFocus, int row, int column) {//// 计算当下行的最佳高度//int maxPreferredHeight = 0;//for (int i = 0; i < table.getColumnCount(); i++) {//setText("" + table.getValueAt(row, i));//setSize(table.getColumnModel().getColumn(column).getWidth(), 0);//maxPreferredHeight = Math.max(maxPreferredHeight,//getPreferredSize().height);//}//return hei += maxPreferredHeight;//}}class PrintPanel extends JPanel implements Printable{@Overridepublic int print(Graphics graphics, PageFormat pageFormat, int pageIndex)throws PrinterException {if (pageIndex >= 1) return Printable.NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) graphics; g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); drawPage1(graphics);return Printable.PAGE_EXISTS;}public void drawPage1(Graphics g2){ JTable table = new JTable(8,3); TableColumn column=null; table.setFont(new Font("Dialog ", 0, 12)); table.getColumnModel().getColumn(0).setMinWidth(100); table.setModel(new javax.swing.table.DefaultTableModel( getObject(), new String[] {"","",""} ) { boolean[] canEdit = new boolean[] {true,true,true}; });// table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer()); table.setSize(table.getPreferredSize()); table.setShowGrid (false);//是否显示网格线 table.setShowHorizontalLines (false);//是否显示水平的网格线 table.setShowVerticalLines (false);//是否显示垂直的网格线 table.paint(g2);}public Object[][] getObject() {Object[][] os = new Object[13][3];for (int i = 0; i < 2; i++) {os[i][0] = "里单元格里单元格";os[i][1] = "里单元格";os[i][2] = "";}return os;}public int getHeight2() { JTable table = new JTable(8,3); TableColumn column=null; table.setFont(new Font("Dialog ", 0, 12)); table.getColumnModel().getColumn(0).setMinWidth(100); table.setModel(new javax.swing.table.DefaultTableModel( getObject(), new String[] {"","",""} ) { boolean[] canEdit = new boolean[] {true,true,true}; }); table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer()); table.setSize(table.getPreferredSize()); table.setShowGrid (false);//是否显示网格线 table.setShowHorizontalLines (false);//是否显示水平的网格线 table.setShowVerticalLines (false);//是否显示垂直的网格线 System.out.println(5*table.getRowCount()+"5*table.getRowCount()"); return 5*table.getRowCount();}} 我本机上装了一个PDF虚拟打印机。以上是测试类的代码,请各位指点一二。如何让打印的内容过长也全部打印出来。 不管我怎么调打印都不全。。请各位指教! 谢谢!!! 问题补充:非常感谢你的帮忙,可以我想要的不是这种。可能是我没有说明白吧! 我指的是每一个单元格里的内容如果过长就打印不全! 就比如 打印“单位格内容单位格内容” 如果字长超过了6个字符就心...表示了! 我想让它自动换行。。。
解决方案
哦,了解了,Qq聊。
解决方案二:
这主要是计算分页的问题。这里写死了,所以总共就打印一页,多的内容就憋到页面外面去了。引用if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;这玩意貌似要根据每页的高度,每行文字的高度来计算出每页可以打印多少行,然后再在print方法里根据pageIndex画出当前页的文本内容。我改写了一下你的PrintPanel类,可以支持多页打印,做例子供参考一下吧,希望能帮的上忙,呵呵。参考资料:http://download.oracle.com/javase/tutorial/2d/printing/set.htmlclass PrintPanel extends JPanel implements Printable {private int[] pageBreaks;//每个分页的开始行号private String[][] lineContents = null;//打印的文本内容private int linesPerPage = 20;//每页的行数,默认20private boolean inited = false;//分页属性是否已初始化public PrintPanel() {initLineContents();}@Overridepublic int print(Graphics graphics, PageFormat pageFormat, int pageIndex)throws PrinterException {if(!inited) {initPageProperties(graphics, pageFormat);}if (pageIndex > pageBreaks.length) {return NO_SUCH_PAGE;}Graphics2D g2 = (Graphics2D) graphics;g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());drawPage1(graphics, getLineContentsByPageIndex(pageIndex));return Printable.PAGE_EXISTS;}public void drawPage1(Graphics g2, String[][] lines) {JTable table = new JTable(8, 3);TableColumn column = null;table.setFont(new Font("Dialog", 0, 12));table.getColumnModel().getColumn(0).setMinWidth(100);table.setModel(new javax.swing.table.DefaultTableModel(lines,new String[] { "", "", "" }) {boolean[] canEdit = new boolean[] { true, true, true };});// table.setDefaultRenderer(Object.class, new// TableCellTextAreaRenderer());table.setSize(table.getPreferredSize());table.setShowGrid(false);// 是否显示网格线table.setShowHorizontalLines(false);// 是否显示水平的网格线table.setShowVerticalLines(false);// 是否显示垂直的网格线table.paint(g2);}public int getHeight2() {JTable table = new JTable(8, 3);TableColumn column = null;table.setFont(new Font("Dialog ", 0, 12));table.getColumnModel().getColumn(0).setMinWidth(100);table.setModel(new javax.swing.table.DefaultTableModel(lineContents,new String[] { "", "", "" }) {boolean[] canEdit = new boolean[] { true, true, true };});table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer());table.setSize(table.getPreferredSize());table.setShowGrid(false);// 是否显示网格线table.setShowHorizontalLines(false);// 是否显示水平的网格线table.setShowVerticalLines(false);// 是否显示垂直的网格线System.out.println(5 * table.getRowCount() + "5*table.getRowCount()");return 5 * table.getRowCount();}//初始化打印内容private void initLineContents() {lineContents = new String[131][3];for (int i = 0; i < 131; i++) {lineContents[i][0] = i + "里单元格里单元格";lineContents[i][1] = "里单元格";lineContents[i][2] = "";}}//初始化分页属性private void initPageProperties(Graphics graphics, PageFormat pageFormat) {//计算每行高度Font font = new Font("Dialog", 0, 12);FontMetrics metrics = graphics.getFontMetrics(font);int lineHeight = metrics.getHeight();//计算分页linesPerPage = (int) (pageFormat.getImageableHeight() / lineHeight - 1);int numBreaks = (lineContents.length - 1) / linesPerPage;pageBreaks = new int[numBreaks];for (int b = 0; b < numBreaks; b++) {pageBreaks[b] = (b + 1) * linesPerPage;}inited = true;}//获取当前页的打印内容private String[][] getLineContentsByPageIndex(int pageIndex) {int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];int end = (pageIndex == pageBreaks.length) ? lineContents.length: pageBreaks[pageIndex];String[][] res = new String[end - start][];for (int i = 0, line = start; line < end; line++, i++) {res[i] = lineContents[line];}return res;}}