📄 buydlg.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
import java.text.SimpleDateFormat;
//采购货物对话框类
class BuyCargoDlg extends JDialog implements ActionListener
{
ConnectionSQL conSql; //数据库连接类
JButton okButn;
JTextField numT, nameT, priceT, countT, cusT, dataT;
BuyCargoDlg(Frame f, String s, boolean b)
{
super(f, s, b);
Container con = getContentPane();
con.setLayout(new BorderLayout());
setSize(250, 330);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); //关闭
okButn = new JButton("确定");
okButn.addActionListener(this);
Date nowTime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"),
dateStr = new SimpleDateFormat("yyyy年MM月dd日");
numT = new JTextField(dateFormat.format(nowTime));
numT.setEditable(false);
nameT = new JTextField(10);
priceT = new JTextField(5);
countT = new JTextField(5);
cusT = new JTextField(10);
dataT = new JTextField(dateStr.format(nowTime));
dataT.setEditable(false);
Box box1, box2, box3, base;
box1 = Box.createVerticalBox();
box1.add(new JLabel("流水号:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("货物名称:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("价格:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("采购量:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("供应商:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("采购日期:"));
box1.add(Box.createVerticalStrut(5));
box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(12));
box2.add(numT);
box2.add(Box.createVerticalStrut(12));
box2.add(nameT);
box2.add(Box.createVerticalStrut(12));
box2.add(priceT);
box2.add(Box.createVerticalStrut(12));
box2.add(countT);
box2.add(Box.createVerticalStrut(12));
box2.add(cusT);
box2.add(Box.createVerticalStrut(12));
box2.add(dataT);
box2.add(Box.createVerticalStrut(12));
box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(18));
box3.add(box1);
box3.add(Box.createHorizontalStrut(10));
box3.add(box2);
box3.add(Box.createHorizontalStrut(18));
base = Box.createVerticalBox();
base.add(box3);
base.add(okButn);
base.add(Box.createVerticalStrut(18));
add(base);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(nameT.getText().trim().equals("") || priceT.getText().trim().equals("") ||
cusT.getText().trim().equals("") || countT.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(this, "有空值!", "提示", JOptionPane.WARNING_MESSAGE);
}
else
{
float price=0;
int count=0;
boolean isdigit=true;
try {
price = Float.parseFloat(priceT.getText());
count = Integer.parseInt(countT.getText());
}
catch(NumberFormatException ee)
{
isdigit = false;
JOptionPane.showMessageDialog(this, "价格和采购量应为数字!", "警告", JOptionPane.WARNING_MESSAGE);
}
if(isdigit)
{
int message = JOptionPane.showConfirmDialog(this, "确定添加吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
if(message == JOptionPane.YES_OPTION && isdigit)
{
String stringSql = String.format("insert into BUY values('%s', '%s', '%f', '%d', '%s', '%s')",
numT.getText(), nameT.getText(), price, count, cusT.getText(), dataT.getText());
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.sql.executeUpdate(stringSql); //插入采购信息
conSql.rs = conSql.sql.executeQuery("select * from CARGO where 名称 = '"+nameT.getText()+"'");
if(conSql.rs.next()) //有记录时执行
{
String updateSql = String.format("update CARGO set 库存量 = 库存量+%d where 名称 = '%s'", count, nameT.getText());
conSql.sql.executeUpdate(updateSql); //更新货物的库存量
}
else //无记录时执行
{
conSql.rs = conSql.sql.executeQuery("select * from CARGO");
int recount=1;
while(conSql.rs.next())
{
recount++; //表中记录数加 1
}
String insertSql = String.format("insert into CARGO(编号,名称,库存量) values('%s', '%s', '%d')",
recount+"", nameT.getText(), count);
conSql.sql.executeUpdate(insertSql); //插入新记录
}
}
catch(SQLException eee) {}
nameT.setText(null); priceT.setText(null);
cusT.setText(null); countT.setText(null);
Date nowTime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"),
dateStr = new SimpleDateFormat("yyyy年MM月dd日");
numT.setText(dateFormat.format(nowTime));
dataT.setText(dateStr.format(nowTime));
}
}
}
}
}
class BuyCargoDel extends JDialog implements ActionListener
{
ConnectionSQL conSql; //
JButton okButn;
JTextField numT, nameT, priceT, countT, cusT, dataT;
BuyCargoDel(Frame f, String s, boolean b)
{
super(f, s, b);
Container con = getContentPane();
con.setLayout(new BorderLayout());
setSize(250, 330);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); //关闭
okButn = new JButton("确定");
okButn.addActionListener(this);
numT = new JTextField(15);
nameT = new JTextField(10);
priceT = new JTextField(5);
countT = new JTextField(5);
cusT = new JTextField(10);
dataT = new JTextField(10);
Box box1, box2, box3, base;
box1 = Box.createVerticalBox();
box1.add(new JLabel("流水号:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("货物名称:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("价格:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("采购量:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("供应商:"));
box1.add(Box.createVerticalStrut(20));
box1.add(new JLabel("采购日期:"));
box1.add(Box.createVerticalStrut(5));
box2 = Box.createVerticalBox();
box2.add(Box.createVerticalStrut(12));
box2.add(numT);
box2.add(Box.createVerticalStrut(12));
box2.add(nameT);
box2.add(Box.createVerticalStrut(12));
box2.add(priceT);
box2.add(Box.createVerticalStrut(12));
box2.add(countT);
box2.add(Box.createVerticalStrut(12));
box2.add(cusT);
box2.add(Box.createVerticalStrut(12));
box2.add(dataT);
box2.add(Box.createVerticalStrut(12));
box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(18));
box3.add(box1);
box3.add(Box.createHorizontalStrut(10));
box3.add(box2);
box3.add(Box.createHorizontalStrut(18));
base = Box.createVerticalBox();
base.add(box3);
base.add(okButn);
base.add(Box.createVerticalStrut(18));
add(base);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
int message = JOptionPane.showConfirmDialog(this, "确定退货吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
if (message == JOptionPane.YES_OPTION)
{
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.rs = conSql.sql.executeQuery("select * from BUY where 流水号 = '"+numT.getText()+"'");
if(conSql.rs.next())
{
conSql.sql.executeUpdate("delete from BUY where 流水号 = '"+numT.getText()+"'");
String updateSql = String.format("update CARGO set 库存量 = 库存量-%d where 名称 = '%s'",
Integer.parseInt(countT.getText()), nameT.getText());
conSql.sql.executeUpdate(updateSql);
}
else
JOptionPane.showMessageDialog(null, "无此记录!\n请查正", "警告", JOptionPane.WARNING_MESSAGE);
}
catch(SQLException ee) {}
numT.setText(null); nameT.setText(null);
priceT.setText(null); countT.setText(null);
cusT.setText(null); dataT.setText(null);
}
}
}
class BuyCargoList extends JDialog implements ActionListener
{
ConnectionSQL conSql;
BuyCargoDlg BuyDlg; //采购货物
BuyCargoDel BuyDel;
JButton all, print, add, del;
JTextField tFrom, tTo;
JTable table;
DefaultTableModel defaultModel = null;
public String numS="", nameS="", priceS="", countS="", cusS="", dataS="";
BuyCargoList(Frame f, String s, boolean b)
{
super(f, s, b);
Container con = getContentPane();
con.setLayout(new BorderLayout());
JPanel pNorth = new JPanel(),
pCenter = new JPanel();
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); //关闭
all = new JButton("全部");
print = new JButton("打印");
add = new JButton("进货");
del = new JButton("退货");
del.setEnabled(false);
all.addActionListener(this);
print.addActionListener(this);
add.addActionListener(this);
del.addActionListener(this);
tFrom = new JTextField(8);
tTo = new JTextField(8);
pNorth.add(new JLabel("从:"));
pNorth.add(tFrom);
pNorth.add(new JLabel("到"));
pNorth.add(tTo);
pNorth.add(all);
pNorth.add(print);
pNorth.add(add);
pNorth.add(del);
Object a[][] = new Object[0][0];
Object n[] = {"流水号","货物名称","进价","采购量","供应商","采购日期"}; //表格的列名
defaultModel = new DefaultTableModel(a,n);
table = new JTable(defaultModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() //
{
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting())
return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (!lsm.isSelectionEmpty())
{
del.setEnabled(true);
int selectedRow = lsm.getMinSelectionIndex();
numS = (String)defaultModel.getValueAt(selectedRow, 0);
nameS = (String)defaultModel.getValueAt(selectedRow, 1);
priceS = (String)defaultModel.getValueAt(selectedRow, 2);
countS = (String)defaultModel.getValueAt(selectedRow, 3);
cusS = (String)defaultModel.getValueAt(selectedRow, 4);
dataS = (String)defaultModel.getValueAt(selectedRow, 5);
}
}
});
pCenter.add(new JScrollPane(table));
con.add(pNorth, BorderLayout.NORTH);
con.add(pCenter, BorderLayout.CENTER);
setSize(600, 500);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==print)
{
JOptionPane.showMessageDialog(this, "无打印机", "提示", JOptionPane.WARNING_MESSAGE);
}
else if(e.getSource()==all)
{
int row = defaultModel.getRowCount()-1; //清空表格***********
while(row>=0)
{
defaultModel.removeRow(row);
defaultModel.setRowCount(row);
row--;
} //*********************************
Object o[] = {"", "", "", "", "",""};
try {
conSql = new ConnectionSQL();
conSql.sql = conSql.con.createStatement();
conSql.rs = conSql.sql.executeQuery("select * from BUY");
while(conSql.rs.next())
{
o[0] = conSql.rs.getString(1);
o[1] = conSql.rs.getString(2);
o[2] = conSql.rs.getString(3);
o[3] = conSql.rs.getString(4);
o[4] = conSql.rs.getString(5);
o[5] = conSql.rs.getString(6);
defaultModel.addRow(o); //增加一行
}
conSql.con.close();
}
catch(SQLException ee)
{
}
}
else if(e.getSource()==add)
{
BuyDlg = new BuyCargoDlg(null, "采购货物", true);
BuyDlg.setLocationRelativeTo(null);
BuyDlg.setVisible(true);
}
else if(e.getSource()==del)
{
BuyDel = new BuyCargoDel(null, "退货", true);
BuyDel.numT.setText(numS); BuyDel.nameT.setText(nameS);
BuyDel.priceT.setText(priceS); BuyDel.countT.setText(countS);
BuyDel.cusT.setText(cusS); BuyDel.dataT.setText(dataS);
BuyDel.setLocationRelativeTo(null);
BuyDel.setVisible(true);
}
}
}
class BuyCargoBList extends JDialog implements ActionListener
{
JButton all, print, add, del;
JTextField tFrom, tTo;
JTable table;
DefaultTableModel defaultModel = null;
BuyCargoBList(Frame f, String s, boolean b)
{
super(f, s, b);
Container con = getContentPane();
con.setLayout(new BorderLayout());
JPanel pNorth = new JPanel(),
pCenter = new JPanel();
//setLocationRelativeTo(null);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); //关闭
all = new JButton("全部");
print = new JButton("打印");
add = new JButton("添加");
del = new JButton("删除");
all.addActionListener(this);
print.addActionListener(this);
add.addActionListener(this);
del.addActionListener(this);
tFrom = new JTextField(8);
tTo = new JTextField(8);
pNorth.add(new JLabel("从:"));
pNorth.add(tFrom);
pNorth.add(new JLabel("到"));
pNorth.add(tTo);
pNorth.add(all);
pNorth.add(print);
pNorth.add(add);
pNorth.add(del);
Object a[][] = new Object[0][0];
Object n[] = {"编号","客户名称","地址","联系人","联系电话"}; //表格的列名
defaultModel = new DefaultTableModel(a,n);
table = new JTable(defaultModel);
pCenter.add(new JScrollPane(table));
con.add(pNorth, BorderLayout.NORTH);
con.add(pCenter, BorderLayout.CENTER);
setSize(600, 500);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if (e.getSource()==print)
{
JOptionPane.showMessageDialog(this, "无打印机", "提示", JOptionPane.WARNING_MESSAGE);
}
else if(e.getSource()==all)
{
}
}
}
public class BuyDlg {
public static void main(String args[])
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -