西工大数据库实验大作业 火锅店菜品管理系统
文章目录
前言:
本学期数据库实验结束了,在最后的大作业阶段,经过本人调研结合所学之后,结合MySQL JDBC Java Swing等技术整合出的此系统,其中相关的学习视频参考链接在:B站学习视频:图书管理系统,本篇将以实验报告的形式对本系统做出相关的介绍
一.大作业简单的需求分析:
本次实验中本人所设计的系统是“河底捞”火锅店的菜品管理系统,“民以食为天”,此系统正是本人在吃饭时寻找到的实验灵感。通过采访和调研火锅店的服务员以及经理,将火锅店背后完整的系统进行分析与设计。其中作为火锅店的经理,即对于菜品部分专门负责的经理需掌握三个方面的管理:第一是关于菜品类别的管理,即火锅店中涉及的菜品需进行分类,如:底料,荤菜,素菜等等,分门别类会更加便于管理;第二是关于菜品的管理,同时也是本系统的核心,一个菜品管理系统中通过记录各种菜品对应的储量,售价,分类等等才能更高效的保证火锅店的经营;第三是供货商管理,针对一个火锅店的菜品,当菜品的储量较低时需要进货,这就牵扯到了供货商进货的问题了,因此将一个菜品管理系统整个就分成这么三部分的管理内容:菜品类别,菜品,供货商,转化为数据库中即将三者分别设计为实体,并通过对于三个实体的增删改查操作来管理和维护整个菜品管理系统。
二.数据流图:
顶层数据流图:
0层数据流图:
三.数据字典:
数据加工:
元素编号 | 名称 | 类型 | 说明 |
---|---|---|---|
B01 | 菜品管理系统 | 处理 | 对菜品类别,菜品和供货商进行管理的系统进行加工 |
B02 | 登录系统 | 处理 | 菜品管理系统的登录界面 |
S01 | 身份信息 | 数据流 | 管理员的身份信息 |
S02 | 菜品类别信息 | 数据流 | 菜品的类别名称以及菜品类别描述 |
S03 | 菜品信息 | 数据流 | 菜品名称,菜品储备,菜品售价,菜品所属类别以及菜品描述 |
S04 | 供货商信息 | 数据流 | 供货商姓名,供货商联系电话,供应菜品以及供应价格 |
IP01 | 菜品类别管理 | 处理 | 添加菜品类别和菜品类别具体管理两个模块 |
IP02 | 菜品管理 | 处理 | 添加菜品和菜品具体管理两个模块 |
IP03 | 供货商管理 | 处理 | 添加供货商和供货商具体管理两个模块 |
IP04 | 菜品类别添加 | 处理 | 添加菜品类别名称以及菜品类别描述 |
IP05 | 菜品类别具体管理 | 处理 | 对菜品类别进行增删改查 |
IP06 | 菜品添加 | 处理 | 添加菜品名称,菜品储备,菜品售价,菜品所属类别以及菜品描述 |
IP07 | 菜品具体管理 | 处理 | 对菜品进行增删改查 |
IP08 | 供货商添加 | 处理 | 添加供货商姓名,供货商联系电话,供应菜品以及供应价格 |
IP09 | 供货商具体管理 | 处理 | 对供应商进行增删改查 |
数据流:
- 数据流名称:S01身份信息:
数据项 | 数据类型 | 长度 | 备注 |
---|---|---|---|
user_id | int | 整型 | 管理员编号 |
user_name | varchar | 45 | 管理员姓名 |
user_password | varchar | 45 | 管理员密码 |
- 数据流名称:S02菜品类别信息:
数据项 | 数据类型 | 长度 | 备注 |
---|---|---|---|
foodtype_id | int | 整型 | 菜品类型编号 |
foodtype_name | varchar | 45 | 菜品类型名称 |
foodtype_desc | varchar | 45 | 菜品类型描述 |
- 数据流名称:S03菜品信息:
数据项 | 数据类型 | 长度 | 备注 |
---|---|---|---|
food_id | int | 整型 | 菜品编号 |
food_name | varchar | 45 | 菜品名称 |
food_storage | int | 整型 | 菜品储备(单位:份) |
food_price | float | 浮点型 | 菜品售价(单位:元/份) |
food_typeid | int | 整型 | 菜品所属类别 |
food_desc | varchar | 45 | 菜品描述 |
- 数据流名称:S04供应商信息:
数据项 | 数据类型 | 长度 | 备注 |
---|---|---|---|
s_id | int | 整型 | 供货商编号 |
s_name | varchar | 45 | 供货商姓名 |
s_tel | varchar | 45 | 供货商电话 |
s_foodid | int | 整型 | 供应菜品编号 |
s_price | float | 浮点型 | 供应价格(单位:元/份) |
四.E-R图:
五.关系模式设计:
在E-R图中出现的实体有:
- 管理员{编号,姓名,密码,(编号->姓名,编号->密码)};
- 菜品类别{编号,名称,描述,(编号->名称,编号->描述)};
- 菜品{编号,名称,储备,售价,所属类别,描述(编号->名称,编号->储备,编号->售价,编号->所属类别,编号->描述)},其中所属类别作为菜品类别中编号的外码;
- 供应商{编号,姓名,电话,供应菜品,供应价,(编号->姓名,编号->电话,编号->供应菜品,编号->供应价)},其中供应菜品作为菜品中编号的外码;
则在上面的描述中对应各个关系实体有:
- 1)在整个设计中各个实体中的属性不可分割且独立,则符合1NF
- 2)各个实体中的属性不存在对码的传递依赖和部分依赖,则符合2NF,3NF
六.各种视图以及各种约束、存储过程、触发器脚本描述:
1.视图:
由于本系统是面向全体管理员的,因此不需要使用视图来进行安全性和查询上面的一些维护
2.约束:
- 在上面的设计中对于每个关系的主键,三个关系中的编号均设置为PRIMARY KEY,并且都要求NOT NULL以及ATUO INCREMENT
- 对于四个关系之间存在两个外键,即:菜品的所属类别与菜品的编号之间,供应商的供应菜品与菜品的编号之间,具体如下:
- 针对某些列中设计的CHECK约束,在建好的表上使用alter添加约束:
-- 菜品储备必须>=0:
alter table food add constraint check_storage check(food_storage>0);
-- 菜品中售价必须>0:
alter table food add constraint check_price check(food_price>0);
-- 供应价格必须>0:
alter table supply add constraint check_price_s check(s_price>0);
运行结果:
3.存储过程:
本存储过程用于更新售价,假定所有菜品的售价是基于供货商供应价而定的,并且利润为40%,即在更新供应价格后结合触发器对于售价进行改变,于是定义的存储过程如下:
delimiter$$
create procedure `changeprice` (in foodid int,in price_in float)
begin
declare price_out float;
select s_price into price_in from supply where s_id = foodid;
set price_out = price_in*1.4;
update food set food_price = price_out where food_id = foodid;
end $$
运行结果:
4.触发器:
本系统中的触发器即是结合上面设计的存储过程在供应商表发生变化时执行上面的存储过程并获得输出,后利用输出更新菜品表中的售价,具体如下:
delimiter $$
-- 添加插入触发器
create trigger `update_price_on_food` after insert on supply
for each row
begin
call `changeprice` (new.s_foodid,new.s_price);
end $$
-- 添加更新触发器
create trigger `update_price_on_food_update` after update on supply
for each row
begin
call `changeprice` (new.s_foodid,new.s_price);
end $$
运行结果:
5.索引设计:
针对数据量较大的关系中频繁使用的查询条件增加索引:菜品中的菜品名称列增加对应的索引,具体如下:
七.大作业环境配置:
- mysql版本:mysql 8.0
- 实现系统界面使用的语言:java
- 使用的IDE:eclipse
- 使用的完成界面的插件:WindowBuilder
八.大作业界面效果演示:
- 登录界面:
- 主界面:
- 关于我们界面:
- 菜品数据维护中的菜单截图:
- 菜品类别添加界面:
- 菜品类别管理界面:
- 菜品添加界面:
- 菜品管理界面:
- 供货商添加界面:
- 供货商信息更改界面:
- 安全退出:
九.关于源代码:
本次在代码编程时使用的是java语言,其中设计符合model-dao-view的模式,并且将JDBC连接和字符串判空的工具类放在utils目录中,将图片放在images目录中,关于代码的结构如下图所示:
1.关于JDBC的演示:
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCtest {
//配置JDBC连接的基本参数:
private static String dbURL = "jdbc:mysql:///bighomework?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8&allowPublicKeyRetrieval=true";
private static String dbUSERNAME = "root";
private static String dbPASSWORD = "123456";
private static String JDBCname="com.mysql.cj.jdbc.Driver";
//获取数据库的连接
public static Connection getCon() throws Exception{
Class.forName(JDBCname);
Connection conn = DriverManager.getConnection(dbURL, dbUSERNAME, dbPASSWORD);
return conn;
}
//关闭数据库连接(占用内存很多)
public static void closeCon(Connection conn) throws Exception{
if(conn!=null){
conn.close();
}
}
public static void main(String[] args) throws Exception{
JDBCtest jdbc = new JDBCtest();
try{
Connection conn = jdbc.getCon();
System.out.println("数据库连接成功!");
System.out.println(conn);
jdbc.closeCon(conn);
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
}
}
2.关于Food实体的演示:
//菜品的实体
public class Food {
private int id; //菜品编号
private String name; //菜品名称
private int storage; //菜品存货(份)
private float price; //菜品售卖价格
private int typeid; //菜品类别id
private String typename; //菜品类别名称
private String desc; //菜品描述
//供货商阶段会用到的构造方法:
public String getTypename() {
return typename;
}
public Food(String name) {
super();
this.name = name;
}
public Food(String name, int typeid) {
super();
this.name = name;
this.typeid = typeid;
}
public void setTypename(String typename) {
this.typename = typename;
}
public Integer getTypeid() {
return typeid;
}
public Food(int id, String name, int storage, float price, int typeid, String desc) {
super();
this.id = id;
this.name = name;
this.storage = storage;
this.price = price;
this.typeid = typeid;
this.desc = desc;
}
public Food(String name, int storage, float price, int typeid, String desc) {
super();
this.name = name;
this.storage = storage;
this.price = price;
this.typeid = typeid;
this.desc = desc;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
public Food() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStorage() {
return storage;
}
public void setStorage(int storage) {
this.storage = storage;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return name;
}
}
3.关于FoodDao类的演示:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import jdbc.model.Food;
import jdbc.utils.StringUtil;
//菜品的Dao类
public class FoodDao {
//菜品添加函数:
public int add(Connection conn,Food food) throws Exception{
String sql = "insert into food values(null,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,food.getName());
pstmt.setInt(2,food.getStorage());
pstmt.setFloat(3,food.getPrice());
pstmt.setInt(4, food.getTypeid());
pstmt.setString(5,food.getDesc());
return pstmt.executeUpdate();
}
//菜品信息查询:
public ResultSet list(Connection conn,Food food)throws Exception {
StringBuffer sb = new StringBuffer("select * from food f,food_type ft where f.food_typeid = ft.foodtype_id");
if(StringUtil.isNotEmpty(food.getName())){
sb.append(" and f.food_name like '%" + food.getName() +"%'");
}
if(food.getTypeid()!= null && food.getTypeid()!=-1 && food.getTypeid()!= 0) {
sb.append(" and f.food_typeid ="+food.getTypeid());
}
PreparedStatement pstmt = conn.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
//菜品信息删除:
public int delete(Connection conn,String id)throws Exception{
String sql = "delete from food where food_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
//菜品信息修改:
public int update(Connection conn,Food food)throws Exception{
String sql = "update food set food_name=?,food_storage=?,food_price=?,food_typeid=?,food_desc=? where food_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, food.getName());
pstmt.setInt(2, food.getStorage());
pstmt.setFloat(3, food.getPrice());
pstmt.setInt(4, food.getTypeid());
pstmt.setString(5, food.getDesc());
pstmt.setInt(6, food.getId());
return pstmt.executeUpdate();
}
//判断菜品类型下是否有菜品:
public boolean existFoodByFoodTypeId(Connection conn,String foodtypeid)throws Exception{
String sql = "select * from food where food_typeid = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, foodtypeid);
ResultSet rs = pstmt.executeQuery();
return rs.next();
}
//初始化供货商下拉框:
public ResultSet list_supply(Connection conn,Food food)throws Exception {
StringBuffer sb = new StringBuffer("select * from food");
if(StringUtil.isNotEmpty(food.getName())){
sb.append(" and foodtype_name like '%"+food.getName()+"%'");
}
PreparedStatement pstmt = conn.prepareStatement(sb.toString().replaceFirst("and","where"));
return pstmt.executeQuery();
}
}
4.关于主界面mainframe设计的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class mainframe extends JFrame {
private JPanel contentPane;
private JDesktopPane table = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainframe frame = new mainframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainframe() {
setTitle("\u201C\u6CB3\u5E95\u635E\u201D\u83DC\u54C1\u7BA1\u7406\u7CFB\u7EDF\u4E3B\u754C\u9762");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1550, 790);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("\u83DC\u54C1\u6570\u636E\u7EF4\u62A4");
mnNewMenu.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u6570\u636E.png"));
menuBar.add(mnNewMenu);
JMenu mnNewMenu_2 = new JMenu("\u83DC\u54C1\u7C7B\u522B\u7BA1\u7406");
mnNewMenu_2.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B\u7BA1\u7406.png"));
mnNewMenu.add(mnNewMenu_2);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u83DC\u54C1\u7C7B\u522B\u6DFB\u52A0");
mntmNewMenuItem_2.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B_\u6DFB\u52A0.png"));
mntmNewMenuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FoodTypeInterFirm foodtypeinterfirm = new FoodTypeInterFirm();
foodtypeinterfirm.setVisible(true);
table.add(foodtypeinterfirm);
}
});
mnNewMenu_2.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u83DC\u54C1\u7C7B\u522B\u7EF4\u62A4");
mntmNewMenuItem_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B_\u7EF4\u62A4.png"));
mntmNewMenuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FoodTypeManageInterFirm foodtypemanageinterfirm = new FoodTypeManageInterFirm();
foodtypemanageinterfirm.setVisible(true);
table.add(foodtypemanageinterfirm);
}
});
mnNewMenu_2.add(mntmNewMenuItem_1);
JMenu mnNewMenu_3 = new JMenu("\u83DC\u54C1\u7BA1\u7406");
mnNewMenu_3.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7BA1\u7406.png"));
mnNewMenu.add(mnNewMenu_3);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u83DC\u54C1\u6DFB\u52A0");
mntmNewMenuItem_3.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1_\u6DFB\u52A0.png"));
mntmNewMenuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FoodAddInterFirm foodaddinterfirm = new FoodAddInterFirm();
foodaddinterfirm.setVisible(true);
table.add(foodaddinterfirm);
}
});
mnNewMenu_3.add(mntmNewMenuItem_3);
JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u83DC\u54C1\u7EF4\u62A4");
mntmNewMenuItem_4.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1_\u7EF4\u62A4.png"));
mntmNewMenuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FoodManageInterFirm foodmanageinterfirm = new FoodManageInterFirm();
foodmanageinterfirm.setVisible(true);
table.add(foodmanageinterfirm);
}
});
mnNewMenu_3.add(mntmNewMenuItem_4);
JMenu mnNewMenu_4 = new JMenu("\u4F9B\u8D27\u5546\u7BA1\u7406");
mnNewMenu_4.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u4F9B\u8D27\u5546.png"));
mnNewMenu.add(mnNewMenu_4);
JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u4F9B\u8D27\u5546\u6DFB\u52A0");
mntmNewMenuItem_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SupplyAddInterFirm supplyAddInterFirm = new SupplyAddInterFirm();
supplyAddInterFirm.setVisible(true);
table.add(supplyAddInterFirm);
}
});
mntmNewMenuItem_6.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u4F9B\u8D27\u5546_\u6DFB\u52A0.png"));
mnNewMenu_4.add(mntmNewMenuItem_6);
JMenuItem mntmNewMenuItem_7 = new JMenuItem("\u4F9B\u8D27\u5546\u4FE1\u606F\u66F4\u6539");
mntmNewMenuItem_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SupplyManageInterFirm supplyManageInterFirm = new SupplyManageInterFirm();
supplyManageInterFirm.setVisible(true);
table.add(supplyManageInterFirm);
}
});
mntmNewMenuItem_7.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u4F9B\u8D27\u5546_\u7EF4\u62A4.png"));
mnNewMenu_4.add(mntmNewMenuItem_7);
JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u5B89\u5168\u9000\u51FA");
mntmNewMenuItem_5.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u9000\u51FA.png"));
mntmNewMenuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否退出系统?");
//System.out.println(result);
if(result == 0) {
dispose();
}
}
});
mnNewMenu.add(mntmNewMenuItem_5);
JMenu mnNewMenu_1 = new JMenu("\u5173\u4E8E\u6211\u4EEC");
mnNewMenu_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u706B\u95051.png"));
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem = new JMenuItem("\u6CB3\u5E95\u635E\u706B\u9505");
mntmNewMenuItem.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u706B\u95052.png"));
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AboutUsInterFirm aboutusinterfirm = new AboutUsInterFirm();
aboutusinterfirm.setVisible(true);
table.add(aboutusinterfirm);
}
});
mnNewMenu_1.add(mntmNewMenuItem);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0,0));
table = new JDesktopPane();
contentPane.add(table,BorderLayout.CENTER);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\mf-bg.jpg"));
lblNewLabel.setBounds(10, 0, 1532, 717);
table.add(lblNewLabel);
//运行设置JFrame最大化:
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
5.关于菜品添加界面FoodAddInterFirm设计的代码:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import jdbc.dao.FoodDao;
import jdbc.dao.FoodTypeDao;
import jdbc.model.Food;
import jdbc.model.FoodType;
import jdbc.utils.JDBCtest;
import jdbc.utils.StringUtil;
public class FoodAddInterFirm extends JInternalFrame {
private JTextField nameTXT;
private JTextField storageTXT;
private JTextField priceTXT;
private JTextField descTXT;
private JComboBox foodtypeCB;
private JDBCtest jdbctest = new JDBCtest();
private FoodTypeDao foodtypedao = new FoodTypeDao();
private FoodDao fooddao = new FoodDao();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FoodAddInterFirm frame = new FoodAddInterFirm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FoodAddInterFirm() {
getContentPane().setBackground(new Color(255, 99, 71));
getContentPane().setForeground(new Color(255, 99, 71));
setClosable(true);
setIconifiable(true);
setTitle("\u83DC\u54C1\u6DFB\u52A0");
setBounds(100, 100, 607, 446);
JLabel lblNewLabel = new JLabel("\u83DC\u54C1\u540D\u79F0\uFF1A");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u8FA3\u6912.png"));
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("等线 Light", Font.PLAIN, 16));
nameTXT = new JTextField();
nameTXT.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u83DC\u54C1\u50A8\u91CF\uFF1A");
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u50A8\u91CF.png"));
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("等线 Light", Font.PLAIN, 16));
storageTXT = new JTextField();
storageTXT.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("\u83DC\u54C1\u552E\u4EF7\uFF1A");
lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u552E\u4EF7.png"));
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("等线 Light", Font.PLAIN, 16));
priceTXT = new JTextField();
priceTXT.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("\u83DC\u54C1\u63CF\u8FF0/\u5907\u6CE8\uFF1A");
lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u7F8E\u98DF.png"));
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("等线 Light", Font.PLAIN, 16));
descTXT = new JTextField();
descTXT.setColumns(10);
//文本域加边框:
descTXT.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
JButton btnNewButton = new JButton("\u6DFB\u52A0");
btnNewButton.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u6DFB\u52A0.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FoodAddActionPerformed(e);
}
});
btnNewButton.setFont(new Font("等线 Light", Font.PLAIN, 16));
JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u91CD\u7F6E.png"));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FoodResetActionPerformed(e);
}
});
btnNewButton_1.setFont(new Font("等线 Light", Font.PLAIN, 16));
JLabel lblNewLabel_4 = new JLabel("\u83DC\u54C1\u7C7B\u522B\uFF1A");
lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B.png"));
lblNewLabel_4.setForeground(new Color(255, 255, 255));
lblNewLabel_4.setFont(new Font("等线 Light", Font.PLAIN, 16));
foodtypeCB = new JComboBox();
foodtypeCB.setBackground(new Color(211, 211, 211));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(105)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addGap(130))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(descTXT, GroupLayout.PREFERRED_SIZE, 475, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel_2, GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
.addComponent(lblNewLabel_1)
.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED))
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblNewLabel)
.addGap(22)))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(nameTXT, GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
.addComponent(foodtypeCB, 0, 142, Short.MAX_VALUE)
.addComponent(priceTXT, GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
.addComponent(storageTXT, 142, 142, Short.MAX_VALUE))
.addGap(222))
.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE))
.addGap(70))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(40)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(nameTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(storageTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(priceTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_2))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_4)
.addComponent(foodtypeCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
.addGap(14)
.addComponent(descTXT, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
.addGap(32)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(btnNewButton_1))
.addContainerGap(44, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
//初始化下拉框:
fillFoodType();
}
//重置事件处理:
private void FoodResetActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
resetValue();
}
//菜品添加事件处理:
private void FoodAddActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
String name = this.nameTXT.getText();
String storage = this.storageTXT.getText();
String price = this.priceTXT.getText();
String desc = this.descTXT.getText();
if(StringUtil.isEmpty(name)) {
JOptionPane.showMessageDialog(null, "菜品名称不能为空!!");
return;
}
if(StringUtil.isEmpty(storage)) {
JOptionPane.showMessageDialog(null, "菜品储备不能为空!!");
return;
}
if(StringUtil.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "菜品售价不能为空!!");
return;
}
FoodType foodtype = (FoodType)foodtypeCB.getSelectedItem();
int foodtype_id = foodtype.getFoodtype_id();
Connection conn = null;
try {
Food food = new Food(name,Integer.parseInt(storage),Float.parseFloat(price),foodtype_id,desc);
conn = jdbctest.getCon();
int addNUM = fooddao.add(conn, food);
if(addNUM == 1) {
JOptionPane.showMessageDialog(null, "菜品添加成功!!");
resetValue();
}
else {
JOptionPane.showMessageDialog(null, "菜品添加失败!!");
}
}catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "菜品添加失败!!");
}finally {
try {
jdbctest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//清空表单
private void resetValue() {
this.nameTXT.setText("");
this.storageTXT.setText("");
this.priceTXT.setText("");
this.descTXT.setText("");
//关于菜品类别有:当没有类别时的处理
if(this.foodtypeCB.getItemCount()>0) {
this.foodtypeCB.setSelectedIndex(0);
}
}
//初始化菜品类别下拉框:
private void fillFoodType() {
Connection conn = null;
FoodType foodtype = null;
try {
conn = jdbctest.getCon();
ResultSet rs = foodtypedao.list(conn, new FoodType());
while(rs.next()) {
foodtype = new FoodType();
foodtype.setFoodtype_id(rs.getInt("foodtype_id"));
foodtype.setFoodtype_name(rs.getString("foodtype_name"));
//需要对方法进行重写:
this.foodtypeCB.addItem(foodtype);
//不直接将字符串传进去是因为传一个对象进去可以获得对应的ID
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
jdbctest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
6.关于菜品信息管理界面FoodManageInterFirm设计的代码:
import java.awt.EventQueue;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import jdbc.dao.FoodDao;
import jdbc.dao.FoodTypeDao;
import jdbc.model.Food;
import jdbc.model.FoodType;
import jdbc.utils.JDBCtest;
import jdbc.utils.StringUtil;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class FoodManageInterFirm extends JInternalFrame {
private JTable foodtable;
private JTextField s_nameTXT;
private JComboBox s_typeCB;
private JTextArea fooddescTXT;
private JComboBox foodtypeCB;
private JDBCtest jdbctest = new JDBCtest();
private FoodTypeDao foodtypedao = new FoodTypeDao();
private FoodDao fooddao = new FoodDao();
private JTextField foodidTXT;
private JTextField foodnameTXT;
private JTextField foodstorageTXT;
private JTextField foodpriceTXT;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FoodManageInterFirm frame = new FoodManageInterFirm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FoodManageInterFirm() {
getContentPane().setBackground(new Color(255, 99, 71));
setIconifiable(true);
setClosable(true);
setTitle("\u83DC\u54C1\u7BA1\u7406");
setBounds(100, 100, 674, 570);
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
panel.setForeground(new Color(255, 255, 255));
panel.setBackground(new Color(255, 99, 71));
panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(255, 99, 71));
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(21)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addComponent(panel_1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE))
.addGap(44))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(25)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
.addContainerGap())
);
JLabel lblNewLabel_2 = new JLabel("\u83DC\u54C1\u7F16\u53F7\uFF1A");
lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u7F16\u53F7.png"));
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("等线 Light", Font.PLAIN, 16));
foodidTXT = new JTextField();
foodidTXT.setEditable(false);
foodidTXT.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("\u83DC\u54C1\u540D\u79F0\uFF1A");
lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u8FA3\u6912.png"));
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("等线 Light", Font.PLAIN, 16));
foodnameTXT = new JTextField();
foodnameTXT.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("\u83DC\u54C1\u50A8\u5907\uFF1A");
lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u50A8\u91CF.png"));
lblNewLabel_4.setFont(new Font("等线 Light", Font.PLAIN, 16));
lblNewLabel_4.setForeground(new Color(255, 255, 255));
foodstorageTXT = new JTextField();
foodstorageTXT.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("\u83DC\u54C1\u552E\u4EF7\uFF1A");
lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u552E\u4EF7.png"));
lblNewLabel_5.setForeground(new Color(255, 255, 255));
lblNewLabel_5.setFont(new Font("等线 Light", Font.PLAIN, 16));
foodpriceTXT = new JTextField();
foodpriceTXT.setColumns(10);
JLabel lblNewLabel_6 = new JLabel("\u83DC\u54C1\u7C7B\u522B\uFF1A");
lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B.png"));
lblNewLabel_6.setForeground(new Color(255, 255, 255));
lblNewLabel_6.setFont(new Font("等线 Light", Font.PLAIN, 16));
foodtypeCB = new JComboBox();
JLabel lblNewLabel_7 = new JLabel("\u83DC\u54C1\u63CF\u8FF0\uFF1A");
lblNewLabel_7.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u7F8E\u98DF.png"));
lblNewLabel_7.setForeground(new Color(255, 255, 255));
lblNewLabel_7.setFont(new Font("等线 Light", Font.PLAIN, 16));
fooddescTXT = new JTextArea();
//文本域加边框:
fooddescTXT.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
JButton btnNewButton_1 = new JButton("\u4FEE\u6539");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
foodUpdateActionPerformed(e);
}
});
btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u4FEE\u6539.png"));
btnNewButton_1.setFont(new Font("等线 Light", Font.PLAIN, 16));
JButton btnNewButton_2 = new JButton("\u5220\u9664");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
foodDeleteActionPerformed(e);
}
});
btnNewButton_2.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u5220\u96642.png"));
btnNewButton_2.setForeground(new Color(0, 0, 0));
btnNewButton_2.setFont(new Font("等线 Light", Font.PLAIN, 16));
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
.addComponent(lblNewLabel_7, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblNewLabel_5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblNewLabel_2, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 82, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addGap(175))
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(foodidTXT, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(lblNewLabel_3))
.addComponent(foodpriceTXT, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(lblNewLabel_6)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(foodtypeCB, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(foodnameTXT, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(lblNewLabel_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(foodstorageTXT, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))))
.addComponent(fooddescTXT, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 483, GroupLayout.PREFERRED_SIZE))
.addGap(104))))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
.addComponent(foodidTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(foodnameTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(foodstorageTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_5)
.addComponent(foodpriceTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_6)
.addComponent(foodtypeCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel_7)
.addComponent(fooddescTXT, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
.addGap(40)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_2)
.addComponent(btnNewButton_1))
.addContainerGap(43, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
JLabel lblNewLabel = new JLabel("\u83DC\u54C1\u540D\u79F0\uFF1A");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u8FA3\u6912.png"));
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("等线 Light", Font.PLAIN, 16));
s_nameTXT = new JTextField();
s_nameTXT.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u83DC\u54C1\u7C7B\u522B\uFF1A");
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u83DC\u54C1\u7C7B\u522B.png"));
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("等线 Light", Font.PLAIN, 16));
s_typeCB = new JComboBox();
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.setIcon(new ImageIcon("C:\\Users\\86135\\eclipse-workspace\\ResturantOrder\\src\\images\\\u67E5\u8BE2.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FoodSearchActionPerformed(e);
}
});
btnNewButton.setFont(new Font("等线 Light", Font.PLAIN, 16));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(s_nameTXT, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(lblNewLabel_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(s_typeCB, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
.addComponent(s_nameTXT, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_1)
.addComponent(s_typeCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))
.addContainerGap())
);
panel.setLayout(gl_panel);
foodtable = new JTable();
foodtable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
foodTableMousePressed(me);
}
});
foodtable.setFont(new Font("等线 Light", Font.PLAIN, 14));
foodtable.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u83DC\u54C1\u7F16\u53F7", "\u83DC\u54C1\u540D\u79F0", "\u83DC\u54C1\u50A8\u5907", "\u83DC\u54C1\u552E\u4EF7", "\u83DC\u54C1\u7C7B\u522B\u7F16\u53F7", "\u83DC\u54C1\u63CF\u8FF0", "\u83DC\u54C1\u7C7B\u522B\u540D\u79F0"
}
));
foodtable.getColumnModel().getColumn(0).setPreferredWidth(63);
foodtable.getColumnModel().getColumn(1).setPreferredWidth(61);
foodtable.getColumnModel().getColumn(2).setPreferredWidth(59);
foodtable.getColumnModel().getColumn(3).setPreferredWidth(61);
foodtable.getColumnModel().getColumn(4).setPreferredWidth(86);
foodtable.getColumnModel().getColumn(6).setPreferredWidth(84);
scrollPane.setViewportView(foodtable);
getContentPane().setLayout(groupLayout);
this.fillFoodType("search");
this.fillFoodType("modify");
this.fillTable(new Food());
}
//菜品删除事件处理
private void foodDeleteActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
String id = foodidTXT.getText();
if(StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录!!");
return;
}
int n = JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
if( n == 0) {
Connection conn = null;
try {
conn = JDBCtest.getCon();
int deleteNUM = fooddao.delete(conn, id);
//JOptionPane.showMessageDialog(null, deleteNUM);
if(deleteNUM != 0) {
JOptionPane.showMessageDialog(null, "删除成功!!");
this.resetValue();
this.fillTable(new Food());
}
else {
JOptionPane.showMessageDialog(null, "删除失败!!");
}
}catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!!");
}finally {
try {
JDBCtest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//修改菜品信息事件处理:
private void foodUpdateActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
String name = this.foodnameTXT.getText();
String storage = this.foodstorageTXT.getText();
String price = this.foodpriceTXT.getText();
String desc = this.fooddescTXT.getText();
String id = this.foodidTXT.getText();
if(StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录!!");
return;
}
if(StringUtil.isEmpty(storage)) {
JOptionPane.showMessageDialog(null, "菜品储备不能为空!!");
return;
}
if(StringUtil.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "菜品售价不能为空!!");
return;
}
if(StringUtil.isEmpty(name)) {
JOptionPane.showMessageDialog(null, "菜品名称不能为空!!");
return;
}
FoodType foodtype = (FoodType)foodtypeCB.getSelectedItem();
int typeid = foodtype.getFoodtype_id();
Food food = new Food(Integer.parseInt(id),name,Integer.parseInt(storage),Float.parseFloat(price),typeid,desc);
Connection conn = null;
try {
conn = jdbctest.getCon();
int addNUM = fooddao.update(conn, food);
if(addNUM == 1) {
JOptionPane.showMessageDialog(null, "菜品修改成功!!");
resetValue();
this.fillTable(new Food());
}
else {
JOptionPane.showMessageDialog(null, "菜品修改失败!!");
}
}catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "菜品修改失败!!");
}finally {
try {
jdbctest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//重置表单事件:
private void resetValue() {
// TODO Auto-generated method stub
this.foodidTXT.setText("");
this.foodnameTXT.setText("");
this.foodstorageTXT.setText("");
this.foodpriceTXT.setText("");
this.fooddescTXT.setText("");
//关于菜品类别有:当没有类别时的处理
if(this.foodtypeCB.getItemCount()>0) {
this.foodtypeCB.setSelectedIndex(0);
}
}
//表格行的鼠标点击事件处理:
private void foodTableMousePressed(MouseEvent me) {
// TODO Auto-generated method stub
int row = this.foodtable.getSelectedRow();
this.foodidTXT.setText((String)foodtable.getValueAt(row, 0));
this.foodnameTXT.setText((String)foodtable.getValueAt(row, 1));
this.foodstorageTXT.setText((String)foodtable.getValueAt(row, 2));
this.foodpriceTXT.setText((String)foodtable.getValueAt(row, 3));
this.fooddescTXT.setText((String)foodtable.getValueAt(row, 5));
String foodtypename = (String)this.foodtable.getValueAt(row, 6);
int n = this.foodtypeCB.getItemCount();
for(int i=0;i<n;i++) {
FoodType item = (FoodType)this.foodtypeCB.getItemAt(i);
if(item.getFoodtype_name().equals(foodtypename)) {
this.foodtypeCB.setSelectedIndex(i);
}
}
}
//菜品搜索查询事件处理:
private void FoodSearchActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
String foodname = s_nameTXT.getText();
FoodType foodtype = (FoodType) this.s_typeCB.getSelectedItem();
int foodtypeid = foodtype.getFoodtype_id();
Food food = new Food(foodname,foodtypeid);
this.fillTable(food);
}
//填充一个下拉框:
private void fillFoodType(String type) {
Connection conn = null;
try {
conn = jdbctest.getCon();
ResultSet rs = foodtypedao.list(conn, new FoodType());
if("search".equals(type)) {
FoodType foodtype = new FoodType();
foodtype.setFoodtype_name("请选择...");
foodtype.setFoodtype_id(-1);
this.s_typeCB.addItem(foodtype);
}
while(rs.next()) {
FoodType foodtype = new FoodType();
foodtype.setFoodtype_name(rs.getString("foodtype_name"));
foodtype.setFoodtype_id(rs.getInt("foodtype_id"));
//分两种情况:
if("search".equals(type)) {
this.s_typeCB.addItem(foodtype);
}else if("modify".equals(type)) {
this.foodtypeCB.addItem(foodtype);
}
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
jdbctest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//初始化表格数据:
private void fillTable(Food food) {
//初始化:清空表格
DefaultTableModel dtm =(DefaultTableModel) foodtable.getModel();
//表格设置成0行:
dtm.setRowCount(0);
Connection conn = null;
try {
conn = jdbctest.getCon();
ResultSet rs = fooddao.list(conn, food);
while(rs.next()) {
Vector v = new Vector();
v.add(rs.getString("food_id"));
v.add(rs.getString("food_name"));
v.add(rs.getString("food_storage"));
v.add(rs.getString("food_price"));
v.add(rs.getString("food_typeid"));
v.add(rs.getString("food_desc"));
v.add(rs.getString("foodtype_name"));
dtm.addRow(v);
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
jdbctest.closeCon(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}