代码如下:
- 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 stub
- System.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{
- @Override
- public 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虚拟打印机。
以上是测试类的代码,请各位指点一二。
如何让打印的内容过长也全部打印出来。 不管我怎么调打印都不全。。
请各位指教! 谢谢!!! |
|