我尝试在NetBeans中打印结算收据。
结算收据是从另一个表数据获取表数据,一切正常。
问题是,当我添加更多行,然后JTable
的长度时,它会生成一个滚动条,滚动条下的数据不会打印。
我想打印完整的JTable
是否在滚动条中。
软件应打印完整的帐单,包括JFrame
和完成JTable
的组件。
这是我的代码:
package project;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class reciept extends javax.swing.JPanel implements Printable{
static DateFormat date=new SimpleDateFormat("dd-M-yyyy");
JFrame f=new JFrame();
static SimpleDateFormat time=new SimpleDateFormat("h:m:s a");
static Date d=new Date(System.currentTimeMillis());
Connection conn;
/**
* Creates new form reciept
*/
public reciept() {
initComponents();
conn=DB.con();
cal();
jTable1.setBackground(Color.white);
}
@Override
public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
if (i > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
reciept.this.printAll(g);
return Printable.PAGE_EXISTS;
}
public void cal(){
reciept.jTable1.getTableHeader().setFont(new Font("Dialog",Font.PLAIN,7));
jTable1.setBackground(Color.decode("#FFFFFF"));
String s=date.format(d);
String ta=time.format(d);
t.setText(ta);
da.setText(s);
jLabel12.setText(sale.ca);
String cas=String.valueOf(sale.bill);
jLabel14.setText(cas);
tb.setText(sale.b);
double sum=0;
for(int i=0; i<jTable1.getRowCount(); i++)
{
String d=jTable1.getValueAt(i, 3).toString();
double d1=Double.parseDouble(d);
sum+=d1;
}
String q1=String.valueOf(sum);
tq.setText(q1);
//-----------------------------------------------------------------------------------------------------------------------
try {
Statement st=conn.createStatement();
String sql="SELECT billnumber From bill";
ResultSet rs=st.executeQuery(sql);
if(rs.next())
{
int bill=rs.getInt("billnumber");
String bnum=String.valueOf(bill);
bn.setText(bnum);
bill++;
st.executeUpdate("UPDATE bill Set billnumber='"+bill+"'");
}
} catch (Exception ex) {
Logger.getLogger(reciept.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
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();
jSeparator1 = new javax.swing.JSeparator();
bn = new javax.swing.JLabel();
da = new javax.swing.JLabel();
t = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
tq = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
tb = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
jLabel14 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jSeparator6 = new javax.swing.JSeparator();
jLabel15 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Object[] col={ "S#","Dis","Price","Qty","Disc","Amt"};
DefaultTableModel tm=new DefaultTableModel(sale.tabledata,col);
jTable1 = new javax.swing.JTable();
jLabel8 = new javax.swing.JLabel();
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/logo.png"))); // NOI18N
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 0, 74, 45));
jLabel2.setFont(new java.awt.Font("Trajan Pro", 1, 21)); // NOI18N
jLabel2.setText("Tayyab Fabrics");
jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 51, -1, -1));
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel3.setText("Shop No. G-1, Rabi Center , Hariyanwala Chowk, D-Ground");
jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 79, 400, -1));
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel4.setText("Ph # 041-8546800");
jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(73, 108, 105, -1));
jLabel5.setFont(new java.awt.Font("Aharoni", 0, 8)); // NOI18N
jLabel5.setText("Sales Receipt");
jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(64, 146, -1, 21));
jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 124, 223, -1));
bn.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
bn.setText("Number # !");
jPanel1.add(bn, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 149, 36, 14));
da.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
da.setText("Date");
jPanel1.add(da, new org.netbeans.lib.awtextra.AbsoluteConstraints(79, 129, 83, 16));
t.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
t.setText("Time");
jPanel1.add(t, new org.netbeans.lib.awtextra.AbsoluteConstraints(147, 146, 85, 21));
jLabel9.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel9.setText("Total Qty");
jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 380, -1, -1));
tq.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
tq.setText("jLabel10");
jPanel1.add(tq, new org.netbeans.lib.awtextra.AbsoluteConstraints(51, 380, 40, 10));
jLabel10.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel10.setText("Total");
jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 381, 31, -1));
tb.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
tb.setText("jLabel11");
jPanel1.add(tb, new org.netbeans.lib.awtextra.AbsoluteConstraints(151, 383, 50, 7));
jSeparator2.setForeground(new java.awt.Color(0, 0, 0));
jPanel1.add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 397, 224, -1));
jLabel14.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel14.setText("jLabel14");
jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 405, 70, 17));
jLabel13.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel13.setText("Cash return");
jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(101, 405, -1, 16));
jLabel12.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel12.setText("jLabel12");
jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(55, 405, 40, 16));
jLabel11.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel11.setText("Cash Recieved");
jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 410, -1, -1));
jSeparator6.setForeground(new java.awt.Color(0, 0, 0));
jPanel1.add(jSeparator6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 479, 237, -1));
jLabel15.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel15.setText("Sold product can be exchanged within 8 days");
jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 428, 209, 14));
jLabel16.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel16.setText("Thank you for Shoping here");
jPanel1.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(37, 448, 160, -1));
jLabel6.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel6.setText("Software Contact:");
jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 464, 65, 9));
jLabel7.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel7.setText("+92 306 909 6975");
jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(97, 464, 90, 9));
jTable1.setFont(new java.awt.Font("Dialog", 0, 6));
jTable1.setModel(tm);
jTable1.setEnabled(false);
jTable1.setFocusable(false);
jTable1.setRowHeight(14);
jScrollPane1.setViewportView(jTable1);
jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 173, 192, 40));
jLabel8.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
jLabel8.setText("Faisalalabad");
jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 92, 58, -1));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel bn;
private javax.swing.JLabel da;
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 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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator6;
public static javax.swing.JTable jTable1;
private javax.swing.JLabel t;
private javax.swing.JLabel tb;
private javax.swing.JLabel tq;
// End of variables declaration
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final reciept pt = new reciept();
f.add(pt, BorderLayout.CENTER);
JButton b = new JButton(new AbstractAction("Print") {
@Override
public void actionPerformed(ActionEvent e) {
PrinterJob pj = PrinterJob.getPrinterJob();
Paper paper = new Paper();
PageFormat pf = pj.defaultPage();
pf.setPaper(paper);
pf.setOrientation(PageFormat.PORTRAIT);
pj.setPrintable(pt, pf);
try {
pj.print();
} catch (PrinterException pe) {
pe.printStackTrace(System.err);
}
}
});
f.getRootPane().setDefaultButton(b);
b.requestFocus();
JPanel p = new JPanel();
p.add(b);
p.setBackground(Color.BLACK);
f.add(p, BorderLayout.NORTH);
f.pack();
f.setVisible(true);
}
});
}
}
如何打印完整的JFrame
以及JTable
的所有行?