当前位置: 技术问答>java相关
如何解决WEB中打印问题?(急!!!)
来源: 互联网 发布时间:2015-04-04
本文导语: 具体为打印table中的内容,而不是整个页面。 哪里能找到相关控件? | 真是些折磨人的小妖精~ 自己看代码吧~ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; import...
具体为打印table中的内容,而不是整个页面。
哪里能找到相关控件?
哪里能找到相关控件?
|
真是些折磨人的小妖精~
自己看代码吧~
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.geom.*;
public class Frame2 extends JFrame implements Printable{
JButton jButton1 = new JButton();
Process pr;
JPopupMenu jPopupMenu1 = new JPopupMenu();
JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(10,5);
JSplitPane jSplitPane1 = new JSplitPane();
TJPanel jPanel1 = new TJPanel();
JLabel jLabel1 = new JLabel();
class TJPanel extends JPanel{
public TJPanel(){
this.setOpaque (true);
}
public void paint(Graphics g){
printTable((Graphics2D)g,jTable1);
}
}
public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jCheckBoxMenuItem1.setText("dff");
this.getContentPane().setLayout(borderLayout1);
jLabel1.setOpaque(true);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("jLabel1");
this.getContentPane().add(jSplitPane1, BorderLayout.CENTER);
this.getContentPane().add(jButton1, BorderLayout.SOUTH);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
jSplitPane1.add(jScrollPane1, JSplitPane.RIGHT );
jSplitPane1.add(jPanel1, JSplitPane.LEFT );
jScrollPane1.getViewport().add(jTable1, null);
jPopupMenu1.add(jCheckBoxMenuItem1);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent
event) {
Object object = event.getSource();
if (object == this) System.exit(0);
}
});
this.setSize (400,300);
jSplitPane1.setDividerLocation(200);
jTable1.setGridColor (Color.black );
}
void jButton1_actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
// Ask user for page format (e.g., portrait/landscape)
// PageFormat pf = job.pageDialog(job.defaultPage());
// Create a landscape page format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// Set up a book
Book bk = new Book();
bk.append(this, job.defaultPage());
// 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) { exc.printStackTrace (); }
}
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
RepaintManager rp = RepaintManager.currentManager (this);
boolean doubleBufferingEnable = rp.isDoubleBufferingEnabled ();
rp.setDoubleBufferingEnabled (false);
this.print(g.create((int)pf.getImageableX (),(int)pf.getImageableY (),(int)pf.getImageableHeight (),(int)pf.getImageableWidth ()));
rp.setDoubleBufferingEnabled (doubleBufferingEnable);
return Printable.PAGE_EXISTS;
}
public void paint(Graphics g){
super.paint(g);
}
public void printTable(Graphics2D g,JTable table){
JTableHeader header = table.getTableHeader ();
int header_height= header.getHeight ();
try{
g.setPaint (Color.white );
g.fill(new Rectangle2D.Double(0, 0,640,480));
BasicStroke back =(BasicStroke) g.getStroke ();
int width = table.getWidth ();
int height = table.getHeight ();
g.setPaint (Color.black);
g.setStroke (new BasicStroke(2.0f));
g.drawRect(0,0,width,height+header_height);
g.setStroke (back);
int column_count = header.getColumnModel().getColumnCount ();
TableColumnModel columnModel=header.getColumnModel ();
int x=0;
int y=20;
FontMetrics fm = g.getFontMetrics ();
Font f = g.getFont ();
int size = f.getSize ();
for(int i=0;i
自己看代码吧~
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.geom.*;
public class Frame2 extends JFrame implements Printable{
JButton jButton1 = new JButton();
Process pr;
JPopupMenu jPopupMenu1 = new JPopupMenu();
JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(10,5);
JSplitPane jSplitPane1 = new JSplitPane();
TJPanel jPanel1 = new TJPanel();
JLabel jLabel1 = new JLabel();
class TJPanel extends JPanel{
public TJPanel(){
this.setOpaque (true);
}
public void paint(Graphics g){
printTable((Graphics2D)g,jTable1);
}
}
public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jCheckBoxMenuItem1.setText("dff");
this.getContentPane().setLayout(borderLayout1);
jLabel1.setOpaque(true);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("jLabel1");
this.getContentPane().add(jSplitPane1, BorderLayout.CENTER);
this.getContentPane().add(jButton1, BorderLayout.SOUTH);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
jSplitPane1.add(jScrollPane1, JSplitPane.RIGHT );
jSplitPane1.add(jPanel1, JSplitPane.LEFT );
jScrollPane1.getViewport().add(jTable1, null);
jPopupMenu1.add(jCheckBoxMenuItem1);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent
event) {
Object object = event.getSource();
if (object == this) System.exit(0);
}
});
this.setSize (400,300);
jSplitPane1.setDividerLocation(200);
jTable1.setGridColor (Color.black );
}
void jButton1_actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
// Ask user for page format (e.g., portrait/landscape)
// PageFormat pf = job.pageDialog(job.defaultPage());
// Create a landscape page format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// Set up a book
Book bk = new Book();
bk.append(this, job.defaultPage());
// 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) { exc.printStackTrace (); }
}
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
RepaintManager rp = RepaintManager.currentManager (this);
boolean doubleBufferingEnable = rp.isDoubleBufferingEnabled ();
rp.setDoubleBufferingEnabled (false);
this.print(g.create((int)pf.getImageableX (),(int)pf.getImageableY (),(int)pf.getImageableHeight (),(int)pf.getImageableWidth ()));
rp.setDoubleBufferingEnabled (doubleBufferingEnable);
return Printable.PAGE_EXISTS;
}
public void paint(Graphics g){
super.paint(g);
}
public void printTable(Graphics2D g,JTable table){
JTableHeader header = table.getTableHeader ();
int header_height= header.getHeight ();
try{
g.setPaint (Color.white );
g.fill(new Rectangle2D.Double(0, 0,640,480));
BasicStroke back =(BasicStroke) g.getStroke ();
int width = table.getWidth ();
int height = table.getHeight ();
g.setPaint (Color.black);
g.setStroke (new BasicStroke(2.0f));
g.drawRect(0,0,width,height+header_height);
g.setStroke (back);
int column_count = header.getColumnModel().getColumnCount ();
TableColumnModel columnModel=header.getColumnModel ();
int x=0;
int y=20;
FontMetrics fm = g.getFontMetrics ();
Font f = g.getFont ();
int size = f.getSize ();
for(int i=0;i