📄 goodsaleframe.java
字号:
package com.aptech.view;
import java.awt.BorderLayout;
import java.awt.*;
import com.aptech.classmarket.*;
import com.aptech.connection.*;
import com.aptech.classmarket.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.sql.Connection;
import java.sql.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class GoodsaleFrame extends JFrame {
String id;
int j=0;
JPanel contentPane;
JLabel lblt = new JLabel();
JLabel lblczy = new JLabel();
JLabel lbl1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel6 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel7 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel8 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Icon mag=new ImageIcon("06.jpg");
JOptionPane jop = new JOptionPane();
JLabel lbltime = new JLabel();
javax.swing.JScrollPane jScrollPane1 = new JScrollPane();
Object[][] cells = { {"", "", "", "", "", "", "", ""}, {"", "", "", "", "",
"", "", ""}, {"", "", "", "", "", "", "", ""}, {"", "",
"", "", "", "", "", ""}, {"", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", ""}, {"", "", "", "", "",
"", "", ""}, {"", "", "", "", "", "", "", ""}, {"", "",
"", "", "", "", "", ""}, {"", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", ""}, {"", "", "", "", "",
"", "", ""}, {"", "", "", "", "", "", "", ""}, {"", "",
"", "", "", "", "", ""}, {"", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", ""}, {"", "", "", "", "",
"", "", ""}, {"", "", "", "", "", "", "", ""}
};
String[] colnames = { "商品名称", "进价", "销售价格","折后价格","销售数量","剩余库存","合计金额"};
javax.swing.JTable jTable1 = new JTable(cells,colnames);
JLabel jLabel1 = new JLabel();
JLabel jLabel4 = new JLabel();
javax.swing.JCheckBox jCheckBox1 = new JCheckBox();
JLabel jLabel5 = new JLabel();
public GoodsaleFrame(String id) {
try {
this.id=id;
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
ActionListener t = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
Timetext time = new Timetext();
String nowtime = time.time();
lbltime.setText(nowtime);
}
};
Timer td=new Timer(1000,t);
td.start();
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.getContentPane().setBackground(Color.cyan);
this.setForeground(SystemColor.control);
setSize(new Dimension(400, 300));
setTitle("一切为了顾客");
lblt.setFont(new java.awt.Font("黑体", Font.BOLD, 20));
lblt.setForeground(Color.red);
lblt.setText("商品销售");
lblt.setBounds(new Rectangle(143, 2, 122, 38));
lblczy.setForeground(Color.blue);
lblczy.setText("操作员:");
lblczy.setBounds(new Rectangle(11, 53, 44, 18));
lbl1.setBounds(new Rectangle(55, 53, 62, 18));
jLabel2.setBounds(new Rectangle(142, 54, 90, 17));
jLabel3.setForeground(Color.blue);
jLabel3.setText("时间:");
jLabel3.setBounds(new Rectangle(142, 53, 38, 20));
jLabel6.setForeground(Color.blue);
jLabel6.setText("商品编号:");
jLabel6.setBounds(new Rectangle(9, 214, 56, 22));
jTextField1.setBounds(new Rectangle(76, 216, 46, 17));
jLabel7.setForeground(Color.blue);
jLabel7.setText("销售数量:");
jLabel7.setBounds(new Rectangle(141, 216, 59, 18));
jTextField2.setBounds(new Rectangle(213, 215, 59, 18));
jLabel8.setForeground(Color.blue);
jLabel8.setText("总计金额:");
jLabel8.setBounds(new Rectangle(108, 234, 68, 34));
jButton1.setBackground(Color.cyan);
jButton1.setBounds(new Rectangle(82, 269, 89, 22));
jButton1.setText("确定");
jButton1.addActionListener(new GoodsaleFrame_jButton1_actionAdapter(this));
jButton2.setBackground(Color.cyan);
jButton2.setBounds(new Rectangle(236, 269, 89, 22));
jButton2.setText("退出");
jButton2.addActionListener(new GoodsaleFrame_jButton2_actionAdapter(this));
jop.setBounds(new Rectangle(60, 333, 228, 42));
lbltime.setBounds(new Rectangle(196, 54, 164, 17));
jScrollPane1.setBounds(new Rectangle(6, 78, 386, 120));
jTable1.setBackground(Color.yellow);
jLabel1.setFont(new java.awt.Font("黑体", Font.BOLD, 18));
jLabel1.setForeground(Color.red);
jLabel1.setBounds(new Rectangle(181, 232, 43, 35));
jLabel4.setText("元");
jLabel4.setBounds(new Rectangle(243, 239, 24, 22));
jCheckBox1.setBackground(Color.cyan);
jCheckBox1.setForeground(Color.blue);
jCheckBox1.setText("是否会员");
jCheckBox1.setBounds(new Rectangle(295, 213, 88, 23));
jLabel5.setForeground(Color.blue);
jLabel5.setText("注:会员8折");
jLabel5.setBounds(new Rectangle(312, 3, 80, 24));
contentPane.add(lblczy);
contentPane.add(lbl1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(lblt);
contentPane.add(jLabel2);
contentPane.add(jLabel3);
contentPane.add(jop);
contentPane.add(lbltime);
contentPane.add(jScrollPane1);
contentPane.add(jLabel6);
contentPane.add(jTextField1);
contentPane.add(jLabel4);
contentPane.add(jLabel1);
contentPane.add(jLabel8);
contentPane.add(jLabel5);
contentPane.add(jLabel7);
contentPane.add(jTextField2);
contentPane.add(jCheckBox1);
jScrollPane1.getViewport().add(jTable1);
jLabel1.setText(0.0+"");
lbl1.setText(id);
}
public void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
}
public void jButton1_actionPerformed(ActionEvent e) {
Tosql ts=new Tosql();
Connection con=ts.tosql();
Selectgoodsid sg=new Selectgoodsid();
boolean flag=false;
Goods g=new Goods();
g.setgoodsid(jTextField1.getText());
flag=sg.select(g);
if(flag==true){
String name="";
float inprice=0;
int amount=0;
float money=0;
try {
PreparedStatement ps = con.prepareStatement(
"select Amount,goodsname,inprice,money from Goods where Goodsid=? ");
ps.setString(1, g.goodsid);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
amount=rs.getInt(1);
name=rs.getString(2);
inprice=rs.getFloat(3);
money=rs.getFloat(4);
}
} catch (SQLException ex) {
}
int saleamount=0;
saleamount=Integer.parseInt(jTextField2.getText());
if(amount>=saleamount&&jCheckBox1.isSelected()==true){
jTable1.setValueAt(name, j, 0);
jTable1.setValueAt(inprice, j, 1);
jTable1.setValueAt(money, j, 2);
jTable1.setValueAt(new DecimalFormat("###,###,###.##").format(money*0.8), j, 3);
jTable1.setValueAt(saleamount,j,4);
jTable1.setValueAt((amount - saleamount),j,5);
jTable1.setValueAt(new DecimalFormat("###,###,###.##").format(money*saleamount*0.8), j, 6);
jLabel1.setText(new DecimalFormat("###,###,###.##").format(money*saleamount*0.8)+"");
try {
PreparedStatement ps = con.prepareStatement(
"update goods set amount=? where goodsid=?");
ps.setInt(1, amount - saleamount);
int i = ps.executeUpdate();
} catch (SQLException ex1) {
}
try {
PreparedStatement p = con.prepareStatement("insert into salegoods(goodsid,goodsname,amount,money,huiyuan,saletime) values(?,?,?,?,?,?)");
p.setString(1, jTextField1.getText());
p.setString(2, name);
p.setInt(3, saleamount);
p.setFloat(4,
Float.parseFloat(jLabel1.getText()));
p.setInt(5,1);
p.setString(6,lbltime.getText());
int i = p.executeUpdate();
} catch (NumberFormatException ex2) {
} catch (SQLException ex2) {
}
jop.showMessageDialog(this, "销售已核算", "系统提示",
JOptionPane.INFORMATION_MESSAGE);
j++;
}
else if(amount>=saleamount&&jCheckBox1.isSelected()==false){
jTable1.setValueAt(name, j, 0);
jTable1.setValueAt(inprice, j, 1);
jTable1.setValueAt(money, j, 2);
jTable1.setValueAt(new DecimalFormat("###,###,###.##").format(money), j, 3);
jTable1.setValueAt(saleamount,j,4);
jTable1.setValueAt((amount - saleamount),j,5);
jTable1.setValueAt(new DecimalFormat("###,###,###.##").format(money*saleamount), j, 6);
jLabel1.setText(new DecimalFormat("###,###,###.##").format(money*saleamount)+"");
try {
PreparedStatement ps = con.prepareStatement(
"update goods set amount=? where goodsid=?");
ps.setInt(1, amount - saleamount);
ps.setString(2,jTextField1.getText());
int i = ps.executeUpdate();
} catch (SQLException ex1) {
}
try {
PreparedStatement p = con.prepareStatement("insert into salegoods(goodsid,goodsname,amount,money,huiyuan,saletime) values(?,?,?,?,?,?)");
p.setString(1, jTextField1.getText());
p.setString(2, name);
p.setInt(3, saleamount);
p.setFloat(4,
Float.parseFloat(jLabel1.getText()));
p.setInt(5,0);
p.setString(6,lbltime.getText());
int i = p.executeUpdate();
} catch (NumberFormatException ex2) {
} catch (SQLException ex2) {
}
jop.showMessageDialog(this, "销售已核算", "系统提示",
JOptionPane.INFORMATION_MESSAGE);
j++;
}
else{
jop.showMessageDialog(this, "数量超出库存数量", "系统提示",
JOptionPane.ERROR_MESSAGE);
}
}
else{
jop.showMessageDialog(this, "无此商品", "系统提示",
JOptionPane.ERROR_MESSAGE);
}
}
}
class GoodsaleFrame_jButton1_actionAdapter implements ActionListener {
private GoodsaleFrame adaptee;
GoodsaleFrame_jButton1_actionAdapter(GoodsaleFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class GoodsaleFrame_jButton2_actionAdapter implements ActionListener {
private GoodsaleFrame adaptee;
GoodsaleFrame_jButton2_actionAdapter(GoodsaleFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -