java swing不打印完整页面

时间:2013-12-11 09:39:04

标签: java swing printing awt

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.table.JTableHeader;

class print  implements ActionListener,KeyListener,Printable
{
 JTable table,stable;
 JTable footer = new JTable(1,6);
 JTable table2= new JTable(11,2);;
 JPanel panel,panell;
 String data[][] = new String[100][6];
 JLabel labelf[]=new JLabel[7];
 JFrame f;
public static void main(String[] args) 
{
   new print();
}
print()
{
    f = new JFrame("Bill Invoice......");
    f.setLayout(null);
    JButton jbe = new JButton("Back");
         //jbe.setFont(labfont3);

        // labfont=new Font("Kunstler Script",Font.BOLD,40);
    JLabel la1=new JLabel("SOLD TO :");
    JLabel la2=new JLabel("PROFORMA INVOICE");
    Font l1=new Font("Times New Roman",Font.BOLD,15);
    Font l2=new Font("Times New Roman",Font.BOLD,18);
    //Font l3=new Font("Times New Roman",Font.BOLD,15);
    la1.setForeground(new Color(138,10,178));
    la2.setForeground(new Color(138,10,178));

    ImageIcon ic2 = new ImageIcon("vv.jpg");
    JLabel piclab2 = new JLabel(ic2);



    JButton button = new JButton("Print");
    JButton button1 = new JButton("Save");

         JButton button2 = new JButton("Finish");
         String[] columnheader = {"S.No.", "Description", "Bales Bags", "Weight in Kgs", "Price ", "Total" };
             labelf[0]=new JLabel("txt10");
             labelf[1]=new JLabel("txt11");
             labelf[2]=new JLabel("txt111");
             labelf[3]=new JLabel("txtt");
             labelf[4]=new JLabel("txt12");
             labelf[5]=new JLabel("txtx");
             labelf[6]=new JLabel("txty");


        table2.getColumnModel().getColumn(0).setPreferredWidth(200);
        table2.getColumnModel().getColumn(1).setPreferredWidth(200);
        table2.setRowHeight(20);
        table2.getModel().setValueAt("INVOICE NO.",0,0);
        table2.getModel().setValueAt("txt00",0,1);
        table2.getModel().setValueAt("DATE",1,0);
        table2.getModel().setValueAt(""+"currentTime",1,1);
        table2.getModel().setValueAt("EXPORTRANS REF.",2,0);
        table2.getModel().setValueAt("txtz",2,1);
        table2.getModel().setValueAt("BOOKING NO.",3,0);
        table2.getModel().setValueAt("txt22",3,1);
        table2.getModel().setValueAt("CONTAINER NO.",4,0);
        table2.getModel().setValueAt("txt33",4,1);
        table2.getModel().setValueAt("SEAL NO.",5,0);
        table2.getModel().setValueAt("txt44",5,1);
        table2.getModel().setValueAt("VESSEL NAME",6,0);
        table2.getModel().setValueAt("txt55",6,1);
        table2.getModel().setValueAt("SHIPPING LINE",7,0);
        table2.getModel().setValueAt("txt66",7,1);
        table2.getModel().setValueAt("BILL OF LANDING NO.",8,0);
        table2.getModel().setValueAt("txt77",8,1);
        table2.getModel().setValueAt("IDF NO.",9,0);
        table2.getModel().setValueAt("txt88",9,1);
        table2.getModel().setValueAt("IO NO.",10,0);
        table2.getModel().setValueAt("txt99",10,1);



        table = new JTable(data,columnheader);
        //table.setTableHeader(columnNames);


        footer.getModel().setValueAt("Total",0,1);

        JTableHeader header = table.getTableHeader();
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(header, BorderLayout.NORTH);
        panel.add(table, BorderLayout.CENTER);
        panel.add(footer,BorderLayout.SOUTH);

         table.getTableHeader().setReorderingAllowed(false);
         table.setRowHeight(25);
         footer.setRowHeight(25);
         table.setShowGrid(false);
         table.setShowVerticalLines(true);


          JScrollPane pane = new JScrollPane(table);
          table.getColumnModel().getColumn(0).setPreferredWidth(24);
          table.getColumnModel().getColumn(1).setPreferredWidth(200);



         //table.getModel().setValueAt(1,0,0);


          table.getTableHeader().setPreferredSize(new Dimension(pane.getWidth(),35));

          footer.getColumnModel().getColumn(0).setPreferredWidth(13);
          footer.getColumnModel().getColumn(1).setPreferredWidth(190);
          footer.getColumnModel().getColumn(2).setPreferredWidth(65);
          footer.getColumnModel().getColumn(3).setPreferredWidth(65);
          footer.getColumnModel().getColumn(4).setPreferredWidth(65);
          footer.getColumnModel().getColumn(5).setPreferredWidth(82);

          jbe.setBounds(0,0,105,30);
          panel.setBounds(180,350,1000,290);
          table2.setBounds(780,100,400,220);
          panel.add(pane, BorderLayout.CENTER);

          la1.setBounds(190,95,150,60);


            int y=140;
                for(int i=0;i<=6;i++)
                {
                labelf[i].setFont(l1);
                labelf[i].setBounds(190,y,500,20);
                y=y+20;
                f.add(labelf[i]);
                }
          la2.setBounds(530,300,300,60);


          button1.setBounds(1250,550,100,30);
          button.setBounds(1250,600,100,30);
          button2.setBounds(1250,650,100,30);

          table.setFont(l1);
          table2.setFont(l1);
          footer.setFont(l1);
          la1.setFont(l2);


          la2.setFont(l2);

          header.setFont(l1);
          f.add(jbe);
          f.add(la1);

          f.add(la2);
          f.add(button);
          f.add(button1);
          f.add(button2);

          button.addActionListener(this);
          button1.addActionListener(this);
          button2.addActionListener(this);
          jbe.addActionListener(this);



          f.add(panel);
          f.add(table2);

          piclab2.setBounds(0,0,1366,768);
          f.add(piclab2);

          f.setSize(1366,768);
          f.setVisible(true);

     }

 @Override
    public void actionPerformed(ActionEvent arg0) 
    {

        String s=arg0.getActionCommand( );

        if (s.equals("Print"))
        {

                    PrinterJob pj = PrinterJob.getPrinterJob();

                    PageFormat pf = pj.defaultPage();
                    Paper paper = new Paper();

                    paper.setImageableArea(50, 100, 400, 200);
                    pf.setPaper(paper);

                    pj.setPrintable(this, pf);

                 if (pj.printDialog()) 
                    {
                      try 
                      {
                            pj.print();

                      } 
                      catch (PrinterException pe)
                          {
                             System.err.println("Error printing: " + pe.getMessage());
                          }

                    }
        }

    }

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


    Rectangle rec = f.getBounds();
    BufferedImage snapshot = new BufferedImage(rec.width,rec.height,BufferedImage.TYPE_INT_ARGB);


    //BufferedImage snapshot = AnimeUtilities.createSnapshotOfFrame(pf, Transparency.TRANSLUCENT);
    double scaleX = pf.getWidth()/snapshot.getWidth();
    double scaleY = pf.getHeight();///snapshot.getHeight();

    double scaleValue = Math.min(scaleX, scaleY);
    System.out.println(""+scaleValue);
    g2d.scale(scaleValue+0.3, scaleValue+0.3);


    table2.print(g2d);


    return (PAGE_EXISTS);



}

@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

我已经尝试了太多但是这段代码只打印了我页面的某些部分......我想要打印整页......我对打印命令并不熟悉.....任何人都有想要打印整页......

2 个答案:

答案 0 :(得分:1)

更改按钮xy,使其显示在form

        button1.setBounds(100, 650, 100, 30);
        button.setBounds(200, 650, 100, 30);
        button2.setBounds(300, 650, 100, 30);

答案 1 :(得分:1)

我假设您要打印用户界面的所有内容。你必须调用frame.print()方法;不是table.print()方法。

感谢。