where and what is jcomponent context?

解决方案 »

  1.   

     
      感谢路人甲。我现在要打印JTable,连带JTableHead.实现分页,每页都要打印相应的表头。下面是我的一个程序带码,请帮我 看一看。程序里还没有处理纵向和横向的问题。选纵向。我现在要打印一个表我计算出它的宽要分2,长分3,共大六页。当打印第1,2页时都没问题,但是打印第3页是,在表头上多打印了一行。这一行是在第一页中打印过的。不知道为什么会这样,你帮我看一看。
      import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.print.*;
    import javax.swing.table.*;public class SimpleBook extends JPanel implements ActionListener{
      private int mNumPagesX;
      private int mNumPagesY;
      private int mNumPages;
      public  double originX = 0;
      public  double originY = 0;
      public JTable table ;
      public JTableHeader head;
      public Paper p;
      final static JButton button = new JButton("Print");  public SimpleBook(JTable a) {
            table = a;
    head = table.getTableHeader();
            button.addActionListener(this);
      }
      public void setNum(PageFormat pageformat)
    {double head_height = head.getHeaderRect(0).getHeight();
    double rowheight  = table.getRowHeight();
    int    rowcount    = table.getRowCount();
    int    rowsofpages = (int)((pageformat.getImageableHeight()-head_height)/rowheight);//count rows of page that i want;
    double imageableheight = rowsofpages*rowheight+head_height; //set imageableheight of paper
    p = pageformat.getPaper();
    double x = p.getImageableX();
    double y = p.getImageableY();
    double imageablewidth = p.getImageableWidth();
    p.setImageableArea(x,y,imageablewidth,imageableheight);
    pageformat.setPaper(p);
    //get the number for page;
    mNumPagesY = rowcount/rowsofpages;
    if((rowcount%rowsofpages)>0)
    mNumPagesY++;
    Rectangle tangle = table.getBounds(null);
    double width = tangle.width;
    mNumPagesX = (int) ((width + pageformat.getImageableWidth() - 1)/ pageformat.getImageableWidth());
    mNumPages = mNumPagesX * mNumPagesY;
      }
      public void actionPerformed(ActionEvent e) 
      {
          PrinterJob job = PrinterJob.getPrinterJob();
      PageFormat pageformat = job.defaultPage();
      setNum(pageformat);
      System.out.println("yuan y"+pageformat.getImageableY());
      Book bk = new Book();
      for(int i=0;i<mNumPages;i++)
        {
              
              bk.append(new PaintCover(), pageformat);
      
            }
        // Pass the book to the PrinterJob
            job.setPageable(bk);
        // Put up the dialog box
            if (job.printDialog()) 
    {
            // Print the job if the user didn't cancel printing
                    try { job.print(); }
                    catch (Exception exc) { /* Handle Exception */ }
                }
          }  
      class PaintCover implements Printable 
    {
          public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException
    {int x = pageIndex % mNumPagesX;
            int y = pageIndex /mNumPagesX;
    originX = (pageFormat.getImageableWidth())*x;
        originY = (pageFormat.getImageableHeight()-head.getHeaderRect(0).getHeight())*y;        Graphics2D g2 = (Graphics2D) graphics;        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()+head.getHeaderRect(0).getHeight());
            g2.translate(-originX,-originY);
      
            table.paint(g2.create(0,0,(int)(originX+pageFormat.getImageableWidth()),(int)(originY+pageFormat.getImageableHeight()-head.getHeaderRect(0).getHeight())));
    g2.dispose();
            g2 = (Graphics2D) graphics;
    g2.translate(0, -head.getHeaderRect(0).getHeight());
    g2.translate(0, originY);
    head.paint(g2.create(0,0,(int)(originX+pageFormat.getImageableWidth()),(int)head.getHeaderRect(0).getHeight()));
    g2.dispose();return PAGE_EXISTS;
        }
      }
    }
    再次感谢路人甲。