Ich habe Anforderung Rechnung zu drucken, die für diese Aufgabe nichts anderes als ein JFrame ist, ich habe verwendet 2 Klassenwie Jframe mit Swing drucken?
Wenn ich auf Bill-Taste (von CreateBill.java Klasse) generieren klicken Sie dann Konstruktor einer anderen Klasse dh GridBagLayoutDemo2 (int id) wird aufgerufen und das Rechnungsfenster wurde jetzt angezeigt. Ich möchte dieses Jframe drucken, wenn ich auf den Druckknopf klicke, aber ich habe keine Idee, dieses Zeug zu machen.
Hier ist meine zwei Klassen
CreateBill.java
class CreateBill extends JPanel implements ActionListener,ItemListener
{
GridBagLayout gbag;
GridBagConstraints cons;
DefaultTableModel model;
static JTable table;
String columnNames [] ={"Sr.No","Particulars","Item Price","Quantity","Amount"};
JComboBox box1,box2,box3,box4,box5,box6,box7;
JButton b1,b2,b3,b4,b5,b6,b7;
String ids,ids1,ids2,ids3,ids4,str1,str2,str3,str4,y;
Vector v1,v2,v3,v4,v5;
Connection con;
Statement stmt;
String vegitm,yourdate;
PreparedStatement ps,ps1,ps2,ps3,ps4;
ResultSet rs,rs1,rs2,rs3,rs4,gs1,gs2,gs3,gs4,rs5,rs6,rs7,rs8,rs9;
static int p;
Float price,Total=0.0f,res;
JLabel lbl;
int index ,k=0,f=0,id=0;
public CreateBill()
{
lbl = new JLabel();
lbl.setFont(new Font("calibri",Font.BOLD,20));
lbl.setForeground(Color.red);
//create GridBagLayout object
gbag = new GridBagLayout();
//set gridbag layout content pane
setLayout(gbag);
//create GridBagConstraints object
cons = new GridBagConstraints();
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("Jdbc:mysql://localhost:3306/hotel?user=root&password=root");
stmt = con.createStatement();
//fetch names from teleenq table
rs = stmt.executeQuery("select fullname from teleenq");
v1 = new Vector();
while(rs.next())
{
ids = rs.getString(1);
v1.add(ids);
}
box1 = new JComboBox(v1);
//fetch common packages from common_pkg table
rs1 = stmt.executeQuery("select pkgName from common_pkg");
v2 = new Vector();
while(rs1.next())
{
ids1 = rs1.getString(1);
v2.add(ids1);
}
box2 = new JComboBox(v2);
box2.addActionListener(this);
//fetch vegcategories from veg_cat
rs2 = stmt.executeQuery("select * from veg_cat");
v3 = new Vector();
while(rs2.next())
{
ids2 = rs2.getString(1);
v3.add(ids2);
}
box3 = new JComboBox(v3);
box3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == box3)
{
String str1 = (String)box3.getSelectedItem();
System.out.println(str1);
try
{
gs1 = stmt.executeQuery("select veg_itemname from veg_catitems where veg_cat ='" + str1 + "'");
int k=0;
while(gs1.next())
{
k++;
}
String[] strArr = new String[k];
gs1.beforeFirst();
int l=0;
while(gs1.next())
{
vegitm = gs1.getString(1);
strArr[l]= vegitm;
l++;
}
box4.setModel(new DefaultComboBoxModel((String[])strArr));
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
}
});
//for vegcat-items
box4 = new JComboBox();
box4.addItem("select veg-items");
box4.addActionListener(this);
//fetch nonveg category from nonveg_cat
rs3 = stmt.executeQuery("select * from nonveg_cat");
v4 = new Vector();
while(rs3.next())
{
ids3 = rs3.getString(1);
v4.add(ids3);
}
box5 = new JComboBox(v4);
box5.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == box5)
{
String str1 = (String)box5.getSelectedItem();
System.out.println(str1);
try
{
gs2 = stmt.executeQuery("select Nonveg_itemname from nonveg_catitems where Nonveg_cat ='" + str1 + "'");
int k=0;
while(gs2.next())
{
k++;
}
String[] strArr = new String[k];
gs2.beforeFirst();
int l=0;
while(gs2.next())
{
vegitm = gs2.getString(1);
strArr[l]= vegitm;
l++;
}
box6.setModel(new DefaultComboBoxModel((String[])strArr));
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
}
});
//for nonveg items
box6 = new JComboBox();
box6.addItem("Select Non-veg items");
box6.addActionListener(this);
box7 = new JComboBox();
// box7.addItem("Select Quantity ");
for(int i=0;i<100;i++)
{
box7.addItem(new Integer(i));
}
box7.addItemListener(this);
}//end of try
catch(Exception e)
{
e.printStackTrace();
}
//create 5 push bottons
b1 = new JButton("ADD Name");
b2 = new JButton("ADD Package");
b2.addActionListener(this);
b3 = new JButton("ADD Veg-items");
b3.addActionListener(this);
b4 = new JButton("ADD Nonveg-items");
b4.addActionListener(this);
//for all buttons , use horizontal filling
cons.fill = GridBagConstraints.FIRST_LINE_START;
//display box1 at x,y cooridnates 0,0
cons.gridx = 0;
cons.gridy = 0;
//set the above constraints to button1
gbag.setConstraints(box1,cons);
//add the button1 to content pane
add(box1);
//display button2 at x,y coordinates 1,0
cons.gridx=1;
cons.gridy =0;
//remaining constatints applicable as set for previous button
//set constraints to button2
gbag.setConstraints(box2, cons);
add(box2);
//display button at x,y coordiantes 2,0
cons.gridx = 2;
cons.gridy = 0;
//remaining constraints applicable as set for previouse button
//set constraints to button3
gbag.setConstraints(box3, cons);
add(box3);
//display button2 at x,y coordinates 1,0
cons.gridx=3;
cons.gridy =0;
//remaining constatints applicable as set for previous button
//set constraints to button2
gbag.setConstraints(box4, cons);
add(box4);
//display button at x,y coordiantes 2,0
cons.gridx = 4;
cons.gridy = 0;
//remaining constraints applicable as set for previouse button
//set constraints to button3
gbag.setConstraints(box5, cons);
add(box5);
cons.gridx=5;
cons.gridy =0;
//remaining constatints applicable as set for previous button
//set constraints to button2
gbag.setConstraints(box6, cons);
add(box6);
//display button at x,y coordiantes 2,0
cons.gridx = 6;
cons.gridy = 0;
//remaining constraints applicable as set for previouse button
//set constraints to button3
gbag.setConstraints(box7, cons);
add(box7);
cons.anchor = GridBagConstraints.LINE_START;
cons.insets = new Insets(10,0,0,0);
//display button 1 at x,y coordinates
cons.gridx = 0;
cons.gridy = 1;
gbag.setConstraints(b1, cons);
b1.addActionListener(this);
add(b1);
//display button 1 at x,y coordinates
cons.gridx = 1;
cons.gridy = 1;
gbag.setConstraints(b2, cons);
add(b2);
//display button 1 at x,y coordinates
cons.gridx = 3;
cons.gridy = 1;
gbag.setConstraints(b3, cons);
add(b3);
//display button 1 at x,y coordinates
cons.gridx = 5;
cons.gridy = 1;
gbag.setConstraints(b4, cons);
add(b4);
model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model);
//table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane sp = new JScrollPane(table);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
cons.anchor = GridBagConstraints.CENTER;
//cons.insets = new Insets(10,150,0,0);
cons.gridx = 3;
cons.gridy = 2;
gbag.setConstraints(lbl, cons);
add(lbl);
cons.anchor = GridBagConstraints.LINE_START;
cons.insets = new Insets(20,0,0,0);
cons.gridx = 0;
cons.gridy = 3;
cons.ipady = 50;
cons.ipadx = 450;
cons.gridwidth = 7;
gbag.setConstraints(sp, cons);
add(sp);
cons.anchor = GridBagConstraints.PAGE_END;
b5 = new JButton("Bill Total");
b5.addActionListener(this);
cons.ipady = 0;
cons.ipadx = 0;
cons.gridx = 0;
cons.gridy = 5;
gbag.setConstraints(b5, cons);
add(b5);
b6 = new JButton("Generate Bill");
b6.addActionListener(this);
cons.gridx = 2;
cons.gridy = 5;
gbag.setConstraints(b6, cons);
add(b6);
b7 = new JButton("Cancel Bill");
b7.addActionListener(this);
cons.gridx = 4;
cons.gridy = 5;
gbag.setConstraints(b7, cons);
add(b7);
setVisible(true);
}//close of constructor
@Override
public void itemStateChanged(ItemEvent e)
{
int state = e.getStateChange();
System.out.println("State :"+state);
if(e.SELECTED == state)
{
index = box7.getSelectedIndex();
}
}
@Override
public void actionPerformed(ActionEvent e)
{
try
{
stmt = con.createStatement();
rs9 = stmt.executeQuery("select * from bill_1");
while(rs9.next())
{
id = rs9.getInt(1);
}
}
catch(Exception ae)
{
ae.printStackTrace();
}
String str1 = " ";
if(e.getSource() == b1)
{
String strName = (String)box1.getSelectedItem();
lbl.setText(strName);
DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
yourdate = dateformat.format(date);
try
{
ps = con.prepareStatement("insert into bill_1(cust_name,Bill_date) values(?,?)");
ps.setString(1,strName);
ps.setString(2,yourdate);
ps.executeUpdate();
} catch (SQLException e1)
{
e1.printStackTrace();
}
}
if(e.getSource() == b2)
{
if(index==0)
{
JOptionPane.showMessageDialog(null,"Please select Qunatity");
}
else
{
str1 = (String)box2.getSelectedItem();
System.out.println("status :"+box2.isEnabled() +" "+"String :"+str1);
if(str1.equals(null))
{
JOptionPane.showMessageDialog(null,"Please select Common Package");
}
else
{
try
{
rs5 = stmt.executeQuery("select package_price from common_pkg where pkgName = '"+str1+"'");
Integer o = (Integer)box7.getSelectedItem();
int qty = o.intValue();
while(rs5.next())
{
p++;
price = rs5.getFloat(1);
res = qty * price;
model.addRow(new Object[]{p,str1,price,qty,res});
}
Total+=res;
ps1 =con.prepareStatement("insert into bill_2 values(?,?,?,?,?,?)");
ps1.setInt(1,p);
ps1.setString(2,str1);
ps1.setFloat(3,price);
ps1.setInt(4,qty);
ps1.setFloat(5,res);
ps1.setInt(6,id);
ps1.executeUpdate();
box7.removeAllItems();
for(int i=0;i<100;i++)
{
box7.addItem(new Integer(i));
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
index = 0;
}
}
}
else if(e.getSource() == b3)
{
String str2 =" ";
if(index==0)
{
JOptionPane.showMessageDialog(null,"Please select Qunatity");
}
else
{
str2 = (String)box4.getSelectedItem();
if(str2.equals(""))
{
JOptionPane.showMessageDialog(null,"Please select Common Package");
}
else
{
if(str2.equals(" "))
{
JOptionPane.showMessageDialog(null,"Please select veg_item");
}
try
{
rs6 = stmt.executeQuery("select veg_itemprice from veg_catitems where veg_itemname = '"+str2+"'");
Integer o = (Integer)box7.getSelectedItem();
int qty = o.intValue();
while(rs6.next())
{
p++;
price = rs6.getFloat(1);
res = qty * price;
model.addRow(new Object[]{p,str2,price,qty,res});
}
Total+=res;
ps1 =con.prepareStatement("insert into bill_2 values(?,?,?,?,?,?)");
ps1.setInt(1,p);
ps1.setString(2,str2);
ps1.setFloat(3,price);
ps1.setInt(4,qty);
ps1.setFloat(5,res);
ps1.setInt(6,id);
ps1.executeUpdate();
box7.removeAllItems();
for(int i=0;i<100;i++)
{
box7.addItem(new Integer(i));
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
index = 0;
}
}
}
else if(e.getSource() == b4)
{
String str3=" ";
if(index==0)
{
JOptionPane.showMessageDialog(null,"Please select Qunatity");
}
else
{
str3 = (String)box6.getSelectedItem();
if(str3.equals(""))
{
JOptionPane.showMessageDialog(null,"Please select Common Package");
}
else
{
System.out.println("status :"+box6.isEnabled() +" "+"String :"+str2);
try
{
rs7 = stmt.executeQuery("select Nonveg_itemprice from nonveg_catitems where Nonveg_itemname = '"+str3+"'");
Integer o = (Integer)box7.getSelectedItem();
int qty = o.intValue();
while(rs7.next())
{
p++;
price = rs7.getFloat(1);
res = qty * price;
model.addRow(new Object[]{p,str3,price,qty,res});
}
Total+=res;
ps1 =con.prepareStatement("insert into bill_2 values(?,?,?,?,?,?)");
ps1.setInt(1,p);
ps1.setString(2,str3);
ps1.setFloat(3,price);
ps1.setInt(4,qty);
ps1.setFloat(5,res);
ps1.setInt(6,id);
ps1.executeUpdate();
box7.removeAllItems();
for(int i=0;i<100;i++)
{
box7.addItem(new Integer(i));
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
index = 0;
}
}
}
if(e.getSource() == b5)
{
if(k==0)
{
//int id = 0;
String s1 = " ";
String s2 = "Total";
model.addRow(new Object[]{s1});
model.addRow(new Object[]{null,null,null,s2,Total});
k++;
try
{
/*stmt = con.createStatement();
rs9 = stmt.executeQuery("select * from bill_1");
while(rs9.next())
{
id = rs9.getInt(1);
}*/
String sql = "update bill_1 set Total = '" + Total + "' where Bill_id ='" + id +"'";
stmt.executeUpdate(sql);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
else
{
JOptionPane.showMessageDialog(null,"Bill is already generated");
}
}
if(e.getSource() == b7)
{
p=0;
while(model.getRowCount()>0)
{
model.removeRow(0);
}
Total=0.0f;
k=0;
}
if(e.getSource() == b6)
{
new GridBagLayoutDemo2(id);
}
}//close action listener
}//close of class
screen shot of CreateBill.java
GridBagLayoutDemo2.java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class GridBagLayoutDemo2 extends JFrame
{
//variables
JFrame frameToPrint;
GridBagLayout gbag;
GridBagConstraints cons;
//variables
JLabel lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7;
JButton b1;
DefaultTableModel model;
Connection con;
Statement stmt;
String vegitm;
PreparedStatement ps,ps1,ps2,ps3,ps4;
ResultSet rs,rs1,rs2,rs3,rs4,gs1,gs2,gs3,gs4,rs5,rs6,rs7,rs8,rs9;
static int p;
Float price,Total=0.0f,res,ft,Item_price,amt;
JLabel lbl;
int index ,k=0,f=0,srno;
int qty;
String s1,s3,s4,particulars,yourdate;
Date s2;
static JTable table;
String columnNames [] ={"Sr.No","Particulars","Item Price","Quantity","Amount"};
CreateBill c;
public GridBagLayoutDemo2(int id)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("Jdbc:mysql://localhost:3306/hotel?user=root&password=root");
stmt = con.createStatement();
//fetch names from teleenq table
rs = stmt.executeQuery("select * from bill_1 where Bill_id = '" + id +"'");
while(rs.next())
{
s1 = rs.getString(2);
s2 = rs.getDate(3);
ft = rs.getFloat(4);
}
//get the content pane
Container c = getContentPane();
//create GridBagLayout object
gbag = new GridBagLayout();
//set gridbag layout to content pane
c.setLayout(gbag);
//create GridBagConstaints object
cons = new GridBagConstraints();
//create Components
lbl1 = new JLabel("4 SEASONS CATERERS");
lbl2 = new JLabel("4 Seasons Farmhouse Society Clibhouse, S. No.436/1 to 441,Bhugaon,Pune");
lbl3 = new JLabel("************************ Mob N0 : 9922863031 **************************");
lbl1.setForeground(Color.DARK_GRAY);
lbl1.setFont(new Font("Calibari",Font.BOLD,35));
lbl2.setFont(new Font("Calibari",Font.BOLD,15));
lbl3.setFont(new Font("Calibari",Font.BOLD,15));
lbl4 = new JLabel("Customer Name :- " +s1);
lbl4.setFont(new Font("Calibari",Font.BOLD,20));
lbl5 = new JLabel("Date:- "+s2);
lbl5.setFont(new Font("Calibari",Font.BOLD,10));
lbl6 = new JLabel("Bill No:-"+id);
lbl6.setFont(new Font("Calibari",Font.BOLD,15));
//for table
model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane sp = new JScrollPane(table);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
rs1 = stmt.executeQuery("select * from bill_2 where Bill_id = '" + id +"'");
String empty = " ";
String st = "Total";
while(rs1.next())
{
srno = rs1.getInt(1);
particulars = rs1.getString(2);
Item_price = rs1.getFloat(3);
qty = rs1.getInt(4);
amt = rs1.getFloat(5);
model.addRow(new Object[]{srno,particulars,Item_price,qty,amt});
}
model.addRow(new Object[]{empty});
model.addRow(new Object[]{null,null,null,st,ft});
//for button
b1 = new JButton("PRINT");
b1.addActionListener(new Paction(frameToPrint));
cons.anchor = GridBagConstraints.PAGE_START;
cons.gridx = 5;
cons.gridy = 0;
gbag.setConstraints(lbl1,cons);
c.add(lbl1);
cons.gridx = 5;
cons.gridy = 1;
gbag.setConstraints(lbl2,cons);
c.add(lbl2);
cons.gridx = 5;
cons.gridy = 2;
gbag.setConstraints(lbl3,cons);
c.add(lbl3);
//add 100-px height-wise
cons.ipady = 60;
cons.gridx = 5;
cons.gridy = 7;
gbag.setConstraints(lbl4,cons);
c.add(lbl4);
cons.anchor = GridBagConstraints.FIRST_LINE_START;
cons.gridx = 2;
cons.gridy = 7;
cons.ipady = 0;
gbag.setConstraints(lbl5,cons);
c.add(lbl5);
cons.gridx = 6;
cons.gridy = 7;
gbag.setConstraints(lbl6,cons);
c.add(lbl6);
cons.anchor = GridBagConstraints.LINE_START;
cons.ipady = 0;
cons.gridx = 5;
cons.gridy = 8;
cons.ipady = 10;
cons.ipadx = 400;
gbag.setConstraints(sp, cons);
add(sp);
cons.anchor = GridBagConstraints.PAGE_END;
cons.ipadx = 0;
cons.gridx = 5;
cons.gridy = 9;
gbag.setConstraints(b1,cons);
c.add(b1);
setSize(1030,900);
setTitle("Bill");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
}
/*public static void main(String[] args)
{
GridBagLayoutDemo2 demo = new GridBagLayoutDemo2();
}*/
class Paction implements ActionListener,Printable
{
JFrame frameToPrint;
@Override
public void actionPerformed(ActionEvent e)
{
PrinterJob job = PrinterJob.getPrinterJob();//get printer job list
job.setPrintable(this);
if(job.printDialog() == true){
try{
job.print();
}
catch(PrinterException ex){
ex.printStackTrace();
}
}
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException,PrinterException
{
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/*
* User (0,0) is typically outside the imageable area, so we must translate
* by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now print the window and its visible contents */
// frameToPrint.printAll(g);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
public Paction(JFrame f){
frameToPrint = f;
}
}
screenshot of GridBagLayoutDemo2.java
Wenn ich auf die Druckschaltfläche klicke, erhalte ich die Druckdialogbox, aber wenn ich sie drucke, erhalte ich eine leere Seite. Wenn jemand eine Idee dazu hat, dann bitte hilf mir, dass ich es in den letzten Tagen versucht habe.
Vielen Dank im Voraus!
Wow Kommentar- !! tolle antwort jetzt bekomme ich meine gewünschte ausgabe danke sehr viel Hut ab !! –
Ich bekomme meine Ausgabe, aber ich habe ein Problem mit dieser Seite nur die Hälfte des Inhalts drucken, was wäre möglich, um es zu lösen !! –
Sie sollten es wie folgt skalieren: 'double scale = Math.min (pf.getImageableWidth()/frameToPrint.getWidth(), pf.getImageableHeight()/frameToPrint.getHeight()); if (Maßstab <1) { AffineTransform at = g2d.getTransform(); g2d.scale (Skala, Skala); frameToPrint.printAll (g2d); g2d.setTransform (at); } sonst { frameToPrint.printAll (g2d); } 'anstelle von' frameToPrint.printAll (g2d); ' – Yev