我有PrintPrew类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.print.*;
import javax.swing.border.*;
public class PrintPrew extends JFrame implements ActionListener, ChangeListener, ItemListener {
JButton print = new JButton("Print"),
cancel = new JButton("Close");
Pageable pg = null;
double scale = 1.0;
JSlider slider = new JSlider();
Page page[] = null;
JComboBox jcb = new JComboBox();
CardLayout cl = new CardLayout();
JPanel p = new JPanel(cl);
public PrintPrew(final Printable pr, final PageFormat p) {
super("Предпросмотр");
this.pg = new Pageable() {
public int getNumberOfPages() {
Graphics g = new java.awt.image.BufferedImage(2,2,java.awt.image.BufferedImage.TYPE_INT_RGB).getGraphics();
int n=0;
try { while(pr.print(g, p, n) == pr.PAGE_EXISTS) n++; }
catch(Exception ex) {ex.printStackTrace();}
return n;
}
public PageFormat getPageFormat(int x) { return p; }
public Printable getPrintable(int x) { return pr; }
};
createPreview();
}
private void createPreview() {
page = new Page[pg.getNumberOfPages()];
FlowLayout fl = new FlowLayout();
PageFormat pf = pg.getPageFormat(0);
Dimension size = new Dimension((int)pf.getPaper().getWidth(), (int)pf.getPaper().getHeight());
if(pf.getOrientation() != PageFormat.PORTRAIT)
size = new Dimension(size.height, size.width);
JPanel temp = null;
for(int i=0; i<page.length; i++) {
jcb.addItem(""+(i+1));
page[i] = new Page(i, size);
p.add(""+(i+1), new JScrollPane(page[i]));
}
setTopPanel();
this.getContentPane().add(p, "Center");
Dimension d = this.getToolkit().getScreenSize();
this.setSize(d.width,d.height-60);
slider.setSize(this.getWidth()/2, slider.getPreferredSize().height);
this.setVisible(true);
page[jcb.getSelectedIndex()].refreshScale();
}
private void setTopPanel() {
FlowLayout fl = new FlowLayout();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JPanel topPanel = new JPanel(gbl), temp = new JPanel(fl); slider.setBorder(new TitledBorder("Percentage Zoom"));
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMinimum(0);
slider.setMaximum(500);
slider.setValue(100);
slider.setMinorTickSpacing(20);
slider.setMajorTickSpacing(100);
slider.addChangeListener(this);
gbc.gridx = 0;
gbc.gridwidth = 1;
gbl.setConstraints(slider, gbc);
topPanel.add(slider);
temp.add(jcb);
temp.add(cancel);
temp.add(print);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbl.setConstraints(temp, gbc);
topPanel.add(temp);
print.addActionListener(this);
cancel.addActionListener(this);
jcb.addItemListener(this);
print.setMnemonic('P');
cancel.setMnemonic('C');
this.getContentPane().add(topPanel, "North");
}
@Override
public void actionPerformed(ActionEvent ae) {
Object o = ae.getSource();
if(o == print) {
try {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.defaultPage(pg.getPageFormat(0));
pj.setPageable(pg);
if(pj.printDialog())
pj.print();
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null,ex.toString(), "Error in Printing",1);
}
}
else if(o == cancel) this.dispose();
}
@Override
public void stateChanged(ChangeEvent ce) {
double temp = (double)slider.getValue()/100.0;
if(temp == scale)
return;
if(temp == 0) temp = 0.01;
scale = temp;
page[jcb.getSelectedIndex()].refreshScale();
this.validate();
}
@Override
public void itemStateChanged(ItemEvent ie) {
cl.show(p, (String)jcb.getSelectedItem());
page[jcb.getSelectedIndex()].refreshScale();
this.validate();
}
public void printCurrentPage() {
try {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.defaultPage(pg.getPageFormat(0));
pj.setPrintable(new PsuedoPrintable());
javax.print.attribute.HashPrintRequestAttributeSet pra =
new javax.print.attribute.HashPrintRequestAttributeSet();
if(pj.printDialog(pra))
pj.print(pra);
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null,ex.toString(), "Error in Printing", 1);
}
}
class Page extends JLabel {
final int n;
final PageFormat pf;
java.awt.image.BufferedImage bi = null;
Dimension size = null;
public Page(int x, Dimension size) {
this.size = size;
bi = new java.awt.image.BufferedImage(size.width, size.height, java.awt.image.BufferedImage.TYPE_INT_RGB);
n = x;
pf = pg.getPageFormat(n);
Graphics g = bi.getGraphics();
Color c = g.getColor();
g.setColor(Color.white);
g.fillRect(0, 0, (int)pf.getWidth(), (int)pf.getHeight());
g.setColor(c);
try {
g.clipRect(0, 0, (int)pf.getWidth(), (int)pf.getHeight());
pg.getPrintable(n).print(g, pf, n);
}
catch(Exception ex) { }
this.setIcon(new ImageIcon(bi));
} public void refreshScale() {
if(scale != 1.0)
this.setIcon(new ImageIcon(bi.getScaledInstance((int)(size.width*scale), (int)(size.height*scale), bi.SCALE_FAST)));
else
this.setIcon(new ImageIcon(bi));
this.validate();
}
}
class PsuedoPrintable implements Printable {
public int print(Graphics g, PageFormat fmt, int index) {
if(index > 0) return Printable.NO_SUCH_PAGE;
int n = jcb.getSelectedIndex();
try { return pg.getPrintable(n).print(g, fmt, n); }
catch(Exception ex) {}
return Printable.PAGE_EXISTS;
}
}
}
和我想要打印的Jpanel类
public class NewJPanel extends javax.swing.JPanel implements Printable {
/**
* Creates new form NewJPanel
*/
public NewJPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jInternalFrame1 = new javax.swing.JInternalFrame();
jInternalFrame2 = new javax.swing.JInternalFrame();
jInternalFrame3 = new javax.swing.JInternalFrame();
jInternalFrame4 = new javax.swing.JInternalFrame();
jLabel17 = new javax.swing.JLabel();
jLabel1.setText("Накладная №");
jLabel2.setText("Марка авто");
jLabel3.setText("Предприятие");
jLabel4.setText("jLabel4");
jLabel5.setText("jLabel5");
jLabel6.setText("Номер авто");
jLabel7.setText("jLabel7");
jLabel8.setText("Водитель");
jLabel9.setText("jLabel9");
jLabel10.setText("Документ №");
jLabel11.setText("jLabel11");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
jLabel12.setText("Примечание");
jLabel13.setText("jLabel13");
jLabel14.setText("Оператор");
jLabel15.setText("jLabel15");
jLabel16.setText("Подпись __________");
jInternalFrame1.setVisible(true);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 60, Short.MAX_VALUE)
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame2.setVisible(true);
javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
jInternalFrame2Layout.setHorizontalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 73, Short.MAX_VALUE)
);
jInternalFrame2Layout.setVerticalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame3.setVisible(true);
javax.swing.GroupLayout jInternalFrame3Layout = new javax.swing.GroupLayout(jInternalFrame3.getContentPane());
jInternalFrame3.getContentPane().setLayout(jInternalFrame3Layout);
jInternalFrame3Layout.setHorizontalGroup(
jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 79, Short.MAX_VALUE)
);
jInternalFrame3Layout.setVerticalGroup(
jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame4.setVisible(true);
javax.swing.GroupLayout jInternalFrame4Layout = new javax.swing.GroupLayout(jInternalFrame4.getContentPane());
jInternalFrame4.getContentPane().setLayout(jInternalFrame4Layout);
jInternalFrame4Layout.setHorizontalGroup(
jInternalFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 86, Short.MAX_VALUE)
);
jInternalFrame4Layout.setVerticalGroup(
jInternalFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 207, Short.MAX_VALUE)
);
jLabel17.setText("jLabel17");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel12)
.addGap(18, 18, 18)
.addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(14, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel14)
.addGap(18, 18, 18)
.addComponent(jLabel15)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel11))))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jInternalFrame3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jInternalFrame4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel17))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel6)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addComponent(jLabel8)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel11))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel12)
.addComponent(jLabel13))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel14)
.addComponent(jLabel15)
.addComponent(jLabel16))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jInternalFrame4)
.addComponent(jInternalFrame2)
.addComponent(jInternalFrame1)
.addComponent(jInternalFrame3))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JInternalFrame jInternalFrame2;
private javax.swing.JInternalFrame jInternalFrame3;
private javax.swing.JInternalFrame jInternalFrame4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) graphics;
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
} else {
this.printAll(g2);
return Printable.PAGE_EXISTS;
}
}
}
但是,如果我将我的printPrew用于我的NewJPanel,它或者不要将任何内容可视化或只是白色列表进行打印。
我需要改变什么?
当我在其他程序中使用我的printPeview表时,它的工作,但在这个程序中,同样的表格只是画了一些线....