“食面埋伏”餐饮管理系统设计与实现--JAVA开发涉及UI界面、POI、连接数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/indigopurple/article/details/70254415

目录

时间安排... 1

代码清单... 2

CaidanS.java(厨师管理菜单的界面)... 2

caidan.java(顾客点菜的菜单界面)... 10

Denglu.java(登录)... 16

Zhuce.java(注册)... 20

dingdanS.java(厨师查看、编辑、删除订单界面)... 25

Home.java(初始主界面)... 29

 

 

时间安排

以下是我的时间安排:

1027

需求分析、功能设计、界面设计、数据库设计

1028

弄清JAVA的布局、容器、监听器等层次、关系和作用,主界面代码实现

1029

连接数据库,登录、注册界面和功能实现,顾客、厨师身份区别

1030

面条、小菜的图片绘制

1031

利用POI读写Excel文件,一键配置菜单功能实现,且保存菜单到数据库

111

堂吃和订座的选座功能实现(桌号状态连接数据库记录)

112

顾客的菜单界面实现,从数据库读取菜单

113

实现顾客从菜单最后一列输入购买数量的功能,监听表格被修改的内容,保存到数据库(即生成订单)

114

实现厨师查看、编辑、删除订单的功能

115

测试与改进

 

代码清单

写了三十多个.java文件,实在懒得一个个打开复制粘贴代码了,而且全放进来的话估计至少有五千行,也看不过来,所以我只放几个核心的。

 

CaidanS.java(厨师管理菜单的界面)

 

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.util.ArrayList;

import java.util.Vector;

 

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTable;

importjavax.swing.filechooser.FileSystemView;

import javax.swing.table.DefaultTableModel;

 

importorg.apache.poi.hssf.usermodel.HSSFSheet;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import java.awt.Color;

import javax.swing.JButton;

import java.awt.Font;

 

public class caidanS {

 

        privateJFrame frame;

        privateJTable noodle;

        privateJTable dish;

        privateJButton setNoodle;

        privateJButton setDish;

       

        ArrayList<noodle>noodles = null;

        ArrayList<dish>dishs = null;

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          caidanSwindow = new caidanS();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publiccaidanS() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("食面埋伏® 菜单 (您的身份是:厨师)");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame.getContentPane().setLayout(null);

                

                 noodle= new JTable(new Object[][]{},new String[]{"类型", "价格","图片"});

                 noodle.setFont(newFont("微软雅黑", Font.PLAIN, 22));

                 noodle.setBackground(Color.ORANGE);

                 noodle.setModel(newDefaultTableModel(

                         newObject[][] {

                         },

                         newString[] {

                                  "类型","价格", "图片"

                         }

                 ){

                         Class[]columnTypes = new Class[] {

                                  String.class,Double.class, ImageIcon.class

                         };

                         publicClass getColumnClass(int columnIndex) {

                                  returncolumnTypes[columnIndex];

                         }

                 });

                 noodle.setBounds(340,50, 500, 300);

                 noodle.setRowHeight(60);

                 noodle.setPreferredSize(null);

                 frame.getContentPane().add(noodle);

                

                 noodle.getColumnModel().getColumn(0).setPreferredWidth(100);

                 noodle.getColumnModel().getColumn(1).setPreferredWidth(50);

                 noodle.getColumnModel().getColumn(2).setPreferredWidth(250);

                

                 dish= new JTable(new Object[][]{},new String[]{"类型", "量","价格","图片"});

                 dish.setFont(newFont("微软雅黑", Font.PLAIN, 22));

                 dish.setBackground(Color.CYAN);

                 dish.setModel(newDefaultTableModel(

                         newObject[][] {

                         },

                         newString[] {

                                  "\u7C7B\u578B","\u91CF", "\u4EF7\u683C", "\u56FE\u7247"

                         }

                 ){

                         Class[]columnTypes = new Class[] {

                                  String.class,String.class, Double.class, ImageIcon.class

                         };

                         publicClass getColumnClass(int columnIndex) {

                                  returncolumnTypes[columnIndex];

                         }

                 });

                 dish.setBounds(340,370, 500, 300);

                 dish.setRowHeight(33);

                 frame.getContentPane().add(dish);

                

                 dish.getColumnModel().getColumn(0).setPreferredWidth(100);

                 dish.getColumnModel().getColumn(1).setPreferredWidth(50);

                 dish.getColumnModel().getColumn(2).setPreferredWidth(50);

                 dish.getColumnModel().getColumn(3).setPreferredWidth(250);

                

                 Stringdriver = "com.mysql.jdbc.Driver";

       String dbName = "NoodleRestaurant";

       String passwrod = "19961225";

       String userName = "root";

       String url = "jdbc:mysql://localhost:3306/" + dbName;

       String sql = "insert into noodle values(?,?,?) ";

                

                 setNoodle= new JButton("\u4E00\u952E\u914D\u7F6E\u9762\u6761");

                 setNoodle.setBounds(900,150, 140, 29);

                 setNoodle.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  try{

                                          noodles= ReadNoodleFile();

                                          DefaultTableModelnoom =  (DefaultTableModel)noodle.getModel();

                                          Class.forName(driver);

                             Connection conn =DriverManager.getConnection(url, userName,

                                     passwrod);

                             PreparedStatement ps =conn.prepareStatement(sql);

                                         

                                          for(noodle n : noodles){

                                                  Vectorv = new Vector(); 

                                          v.add(n.name); 

                                          v.add(n.price); 

                                         

                                          File f = new File(n.path);

                                          if (f.exists()) {

                                              v.add(new ImageIcon(n.path)); 

                                              ps.setString(3, n.path);

                                          }

                                         

                                          ps.setString(1, n.name); 

                                          ps.setDouble(2, n.price);

                                          ps.executeUpdate(); 

                                         

                                          noom.addRow(v); 

                                    

                                     }

                                          ps.close(); 

                                 conn.close();

                                

                                 intoption=JOptionPane.showConfirmDialog(null, "面条配置成功!\n是否返回主页面?");

                                          if(option==JOptionPane.YES_OPTION){

                                                  frame.dispose();

                                                  HomeS.main(null);

                                          }

                                          }catch(Exception e1) { 

                                     System.out.println(e1); 

                                     } 

                                          }

                 });

                 frame.getContentPane().add(setNoodle);

                

                 setDish= new JButton("\u4E00\u952E\u914D\u7F6E\u5C0F\u83DC");

                 setDish.setBounds(900,500, 140, 29);

                 setDish.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  try{

                                          dishs= ReadDishFile();

                                          DefaultTableModeldism =  (DefaultTableModel)dish.getModel();

                                         

                                          Stringsql2 = "insert into dish values(?,?,?,?) ";

                                          Class.forName(driver);

                             Connection conn = DriverManager.getConnection(url,userName,

                                     passwrod);

                             PreparedStatement ps2 =conn.prepareStatement(sql2);

                                         

                                          for(dish d : dishs){

                                                  Vectorv2 = new Vector(); 

                                          v2.add(d.name); 

                                          v2.add(d.amount);

                                          v2.add(d.price); 

                                         

                                          File f2 = new File(d.path);

                                          if (f2.exists()) {

                                              v2.add(new ImageIcon(d.path));  

                                              ps2.setString(4, d.path);

                                          }

                                         

                                          ps2.setString(1, d.name); 

                                          ps2.setString(2, d.amount);

                                          ps2.setDouble(3, d.price);

                                          ps2.executeUpdate();

                                         

                                          dism.addRow(v2);

                                          }

                                          ps2.close(); 

                                 conn.close();

                                 int option2=JOptionPane.showConfirmDialog(null,"小菜配置成功!\n是否返回主页面?");

                                          if(option2==JOptionPane.YES_OPTION){

                                                  frame.dispose();

                                                  HomeS.main(null);

                                          }

                                  }catch (Exception e1) {

                                          //TODO Auto-generated catch block

                                          e1.printStackTrace();

                                  }

                         }

                });

                 frame.getContentPane().add(setDish);

                

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\noodle\\bg.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

                

                

        }

       

        publicstatic ArrayList<noodle> ReadNoodleFile() throws Exception {

                 //TODO Auto-generated method stub

                

                 JFileChooserfileChooser = new JFileChooser();

                 ArrayListtemp = new ArrayList();

                

                 if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                         java.io.Filefile = fileChooser.getSelectedFile();

                        

                         FileInputStreamfileIn = new FileInputStream(file);

                         //根据指定的文件输入流导入Excel从而产生Workbook对象

                         HSSFWorkbookwb0 = new HSSFWorkbook(fileIn);

                         //获取Excel文档中的第一个表单

                         HSSFSheetsht0 = wb0.getSheetAt(0);

                         //对Sheet中的每一行进行迭代

                        

                         intr;

                         introwNum = sht0.getPhysicalNumberOfRows();

                        

                         for(r = 1; r <= rowNum; r++) {

                                 

                                  Rowrow = sht0.getRow(r);

                                  if(row == null) {

                                          break;

                                  }

                                 

                                  //创建实体类

                                  noodleinfo=new noodle();

                                 

                                  //取出当前行第1个单元格数据,并封装在info实体stuName属性上

                                  info.setName(row.getCell(0).getStringCellValue());

                                  info.setPrice(row.getCell(1).getNumericCellValue());

                                  info.setPath(row.getCell(2).getStringCellValue());

                                  temp.add(info);

                                    

                         }

                         fileIn.close();  

                 }

                        

                 else{

                         System.out.println("Nofile selected");

                 }

                 returntemp;

                 }

       

        publicstatic ArrayList<dish> ReadDishFile() throws Exception {

                 //TODO Auto-generated method stub

                

                 JFileChooserfileChooser = new JFileChooser();

                 ArrayListtemp = new ArrayList();

                

                 if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                         java.io.Filefile = fileChooser.getSelectedFile();

                        

                         FileInputStream fileIn = newFileInputStream(file);

                         //根据指定的文件输入流导入Excel从而产生Workbook对象

                         HSSFWorkbookwb0 = new HSSFWorkbook(fileIn);

                         //获取Excel文档中的第一个表单

                         HSSFSheetsht0 = wb0.getSheetAt(0);

                         //对Sheet中的每一行进行迭代

                        

                         intr;

                         introwNum = sht0.getPhysicalNumberOfRows();

                        

                         for(r = 1; r <= rowNum; r++) {

                                 

                                  Rowrow = sht0.getRow(r);

                                  if(row == null) {

                                          break;

                                  }

                                 

                                  //创建实体类

                                  dishinfo=new dish();

                                 

                                  //取出当前行第1个单元格数据,并封装在info实体stuName属性上

                                  info.setName(row.getCell(0).getStringCellValue());

                                  info.setAmount(row.getCell(1).getStringCellValue());

                                  info.setPrice(row.getCell(2).getNumericCellValue());

                                  info.setPath(row.getCell(3).getStringCellValue());

                                  temp.add(info);

                                    

                         }

                         fileIn.close();  

                 }

                        

                 else{

                         System.out.println("Nofile selected");

                 }

                 returntemp;

                 }

}

 

caidan.java(顾客点菜的菜单界面)

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.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Vector;

 

import javax.swing.CellEditor;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.JButton;

import javax.swing.JTextPane;

 

/* 顾客看到的菜单界面 --ZYP */

 

public class caidan {

 

        privateJFrame frame;

        privateJTable noodle;

        privateJTable dish;

        privateJButton buy;

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          caidanwindow = new caidan();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publiccaidan() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("\u98DF\u9762\u57CB\u4F0F\u00AE\u83DC\u5355 \uFF08\u60A8\u7684\u8EAB\u4EFD\u662F\uFF1A\u987E\u5BA2\uFF09");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame.getContentPane().setLayout(null);

                

                 dish= new JTable();

                 dish.setFont(newFont("微软雅黑", Font.PLAIN, 22));

                 dish.setBackground(Color.CYAN);

                 dish.setModel(newDefaultTableModel(new Object[][] {

                 },

                 newString[] {

                         "\u7C7B\u578B","\u91CF", "\u4EF7\u683C", "\u56FE\u7247","\u6570\u91CF"

                 }){

                         Class[]columnTypes = new Class[] {

                                  String.class,String.class, Double.class, ImageIcon.class, Integer.class

                         };

                         publicClass getColumnClass(int columnIndex) {

                                  returncolumnTypes[columnIndex];

                         }

                 });

                 dish.setBounds(340,370, 500, 300);

                 dish.setRowHeight(33);

                 frame.getContentPane().add(dish);

                dish.getColumnModel().getColumn(0).setPreferredWidth(100);

                 dish.getColumnModel().getColumn(1).setPreferredWidth(50);

                 dish.getColumnModel().getColumn(2).setPreferredWidth(50);

                 dish.getColumnModel().getColumn(3).setPreferredWidth(250);

                 dish.getColumnModel().getColumn(3).setPreferredWidth(50);

                

                 noodle= new JTable();

                 noodle.setFont(newFont("微软雅黑", Font.PLAIN, 22));

                 noodle.setBackground(Color.ORANGE);

                 noodle.setModel(newDefaultTableModel(

                                  newObject[][] {

                                  },

                                  newString[] {

                                          "\u7C7B\u578B","\u4EF7\u683C", "\u56FE\u7247", "\u6570\u91CF"

                                  }

                         ){

                         Class[]columnTypes = new Class[] {

                                  String.class,Double.class, ImageIcon.class, Integer.class

                         };

                         publicClass getColumnClass(int columnIndex) {

                                  returncolumnTypes[columnIndex];

                         }

                 });

                 noodle.setBounds(340,50, 500, 300);

                 noodle.setRowHeight(60);

                 frame.getContentPane().add(noodle);

                 noodle.getColumnModel().getColumn(0).setPreferredWidth(100);

                 noodle.getColumnModel().getColumn(1).setPreferredWidth(50);

                noodle.getColumnModel().getColumn(2).setPreferredWidth(250);

                 noodle.getColumnModel().getColumn(3).setPreferredWidth(50);

                 /*表格列宽根据窗口大小调整比例自适应  --ZYP */

 

                 JOptionPane.showMessageDialog(null,"请在菜单每行末尾\n修改您的购买数量^_^");

                

                 /*设置按钮,点击后保存表格修改内容到数据库  --ZYP*/

                

                 buy= new JButton("\u786E\u8BA4\u8D2D\u4E70");

                 buy.setBounds(900,350, 123, 29);

                 buy.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  testt = new test();

                                  t.orderID++;

                                 

                                  try{

                         intnrow = noodle.getSelectedRow();

                         intncol = noodle.getSelectedColumn();

                         intdrow = dish.getSelectedRow();

                         intdcol = dish.getSelectedColumn();

                         CellEditornce = noodle.getCellEditor(nrow, ncol);

                         nce.stopCellEditing();

                         CellEditordce = dish.getCellEditor(drow, dcol);

                         dce.stopCellEditing();

                         }catch(Exception e1) { 

                                     System.out.println(e1); 

                                     }

                                 

                                  Stringdriver = "com.mysql.jdbc.Driver";

                         String dbName ="NoodleRestaurant";

                         String passwrod = "19961225";

                         String userName = "root";

                         String url ="jdbc:mysql://localhost:3306/" + dbName;

                         String sql = "insert intoorderDetail values(?,?,?,?,?,?)";

                        

                         try {

                        

                         DefaultTableModel noom =  (DefaultTableModel) noodle.getModel();

                         DefaultTableModel dism =  (DefaultTableModel) dish.getModel();

                                  Class.forName(driver);

                    Connection conn =DriverManager.getConnection(url, userName,

                            passwrod);

                    PreparedStatement ps =conn.prepareStatement(sql);

                    PreparedStatement ps2 =conn.prepareStatement(sql);

                        

                                  inti , num;

                                  intj = noom.getRowCount();

                                  intk = dism.getRowCount();

                                  for(i = 0; i < j; i++) {

                                          num= (int) noom.getValueAt(i, 3);

                                          if(num != 0)

                                          {

                                                  ps.setInt(1,t.orderID);

                                                  ps.setString(2,(String) noom.getValueAt(i, 0));

                                                  ps.setString(3, null);

                                                  ps.setDouble(4,(double) noom.getValueAt(i, 1));

                                                  ps.setInt(5,(int) noom.getValueAt(i, 3));

                                                  ps.setDouble(6,((double) noom.getValueAt(i, 1) * (int)noom.getValueAt(i, 3)));

                                                 

                                                  ps.executeUpdate();

                                          }

                                  }

                                          ps.close();

                                 

                                         

                                  for(i = 0; i < k; i++) {

                                          num= (int) dism.getValueAt(i, 4);

                                          if(num != 0)

                                          {

                                                  ps2.setInt(1,t.orderID);

                                                  ps2.setString(2,(String) dism.getValueAt(i, 0));

                                                  ps2.setString(3,(String) dism.getValueAt(i, 1));

                                                  ps2.setDouble(4,(double) dism.getValueAt(i, 2));

                                                  ps2.setInt(5,(int) dism.getValueAt(i, 4));

                                                  ps2.setDouble(6,((double) dism.getValueAt(i, 2) * (int)dism.getValueAt(i, 4)));

                                                 

                                                  ps2.executeUpdate();

                                          }

                                  }

                                         

                                  ps2.close(); 

                             conn.close();

                            

                             intoption=JOptionPane.showConfirmDialog(null, "购买成功!\n是否返回主页面?");

                                  if(option==JOptionPane.YES_OPTION){

                                          frame.dispose();

                                          HomeB.main(null);

                                          return;

                                  }

                                 

                         }catch (Exception e1) { 

                             System.out.println(e1); 

                             }

                         }

                 });

                 frame.getContentPane().add(buy);

 

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\noodle\\bg.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

                

                 Stringdriver = "com.mysql.jdbc.Driver";

       String dbName = "NoodleRestaurant";

       String passwrod = "19961225";

       String userName = "root";

       String url = "jdbc:mysql://localhost:3306/" + dbName;

       String sql = "select * from noodle";

       String sql2 = "select * from dish";

       

       try {

                        

                         DefaultTableModelnoom =  (DefaultTableModel)noodle.getModel();

                         DefaultTableModeldism =  (DefaultTableModel)dish.getModel();

                         Class.forName(driver);

           Connection conn = DriverManager.getConnection(url, userName,

                    passwrod);

           PreparedStatement ps = conn.prepareStatement(sql);

           PreparedStatement ps2 = conn.prepareStatement(sql2);

           

           ResultSet rs = ps.executeQuery(sql);

           while (rs.next()) {

            Vector v = new Vector();

                                  v.add(rs.getString(1)); 

                         v.add(rs.getDouble(2));

                         v.add(new ImageIcon(rs.getString(3)));

                         v.add(0);

                         noom.addRow(v);

                         }

           rs.close();

           ps.close();

           

           ResultSet rs2 = ps2.executeQuery(sql2);

           while (rs2.next()) {

            Vector v = new Vector();

                                  v.add(rs2.getString(1));

                                  v.add(rs2.getString(2));

                         v.add(rs2.getDouble(3));

                         v.add(new ImageIcon(rs2.getString(4)));

                         v.add(0);

                         dism.addRow(v);

                         }

           rs2.close();

           ps2.close();

           conn.close();

       }catch (SQLException e) {

                                  System.out.println("MySQL操作错误");

                                  e.printStackTrace();

                         }catch (Exception e) {

                                  e.printStackTrace();

                         }finally {

                                 

                         }

       

       

        }

}

 

Denglu.java(登录)

 

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import java.awt.Color;

 

public class denglu {

 

        privateJFrame frame;

        privateJTextField yhm;

        privateJPasswordField mm;

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          dengluwindow = new denglu();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publicdenglu() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("\u98DF\u9762\u57CB\u4F0F\u00AE\u767B\u5F55");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame.getContentPane().setLayout(null);

                

                 yhm= new JTextField();

                 yhm.setText("");

                 yhm.setBounds(600,148, 200, 27);

                 frame.getContentPane().add(yhm);

                 yhm.setColumns(10);

                

                 mm= new JPasswordField();

                 mm.setText("");

                 mm.setBounds(600,255, 200, 27);

                 frame.getContentPane().add(mm);

                

                 JButtonloginButton = new JButton("\u767B\u5F55");

                 loginButton.setBackground(Color.CYAN);

                loginButton.setBounds(575, 500, 123,29);

                 frame.getContentPane().add(loginButton);

                 loginButton.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent a) {

                                  //TODO Auto-generated method stub

                                  Stringyh = yhm.getText(),

                                                  psw= mm.getText();

                                  Stringdriver = "com.mysql.jdbc.Driver";

                         String dbName ="NoodleRestaurant";

                         String passwrod = "19961225";

                         String userName = "root";

                         String url ="jdbc:mysql://localhost:3306/" + dbName;

                         String sql = "select * from userwhere ID =?";

                        

                         int status;

                        

                         try {

                             Class.forName(driver);

                             Connection conn =DriverManager.getConnection(url, userName,

                                     passwrod);

                             PreparedStatement ps =conn.prepareStatement(sql);

                             ps.setString(1, yh);

                             ResultSet rs = ps.executeQuery();

                             if (!rs.next()) {

                             JOptionPane.showMessageDialog(null,"用户名不存在!");

                             return;

                             }

                             else if(!(psw.equals(rs.getString(3)))) {

                             JOptionPane.showMessageDialog(null,"密码错误!");

                             return;

                             }

                             else {

                             status= rs.getInt(4);

                            if(status == 1){

                                       test.flag  = 1;

                                       JOptionPane.showMessageDialog(null,"来掌管你的世界吧,伟大的厨师!");

                             }

                             if(status == 0) {

                                       test.flag= 0;

                                    JOptionPane.showMessageDialog(null,"欢迎您回来,祝您用餐愉快!");

                             }

                             frame.dispose();

                             test.testHome();

                             }

                            

 

                             // 关闭记录集

                             if (rs != null) {

                                 try {

                                     rs.close();

                                 } catch (SQLException e) {

                                     e.printStackTrace();

                                 }

                             }

                  

                             // 关闭声明

                             if (ps != null) {

                                 try {

                                     ps.close();

                                 } catch (SQLException e) {

                                     e.printStackTrace();

                                 }

                             }

                  

                             // 关闭链接对象

                             if (conn != null) {

                                 try {

                                     conn.close();

                                 } catch (SQLException e) {

                                     e.printStackTrace();

                                 }

                             }

                  

                         } catch (Exception e) {

                             e.printStackTrace();

                         }

                         }

                 });

                

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\noodle\\denglu.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

        }

 

}

 

Zhuce.java(注册)

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

import javax.swing.JRadioButton;

import javax.swing.ButtonGroup;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import java.awt.Color;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class zhuce {

 

        privateJFrame frame;

        privateJTextField nicheng;

        privateJTextField yhm;

        privateJPasswordField mm;

        privateJPasswordField qrmm;

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          zhucewindow = new zhuce();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publiczhuce() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("\u98DF\u9762\u57CB\u4F0F\u00AE\u6CE8\u518C");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame.getContentPane().setLayout(null);

                

                 nicheng= new JTextField();

                 nicheng.setBounds(600,385, 200, 27);

                 frame.getContentPane().add(nicheng);

                

                 yhm= new JTextField();

                 yhm.setBounds(600,128, 200, 27);

                 frame.getContentPane().add(yhm);

                

                 mm= new JPasswordField();

                 mm.setBounds(600,215, 200, 27);

                 frame.getContentPane().add(mm);

                

                 qrmm= new JPasswordField();

                 qrmm.setBounds(600,299, 200, 27);

                 frame.getContentPane().add(qrmm);

                

                 JRadioButtongukeButton = new JRadioButton("\u987E\u5BA2");

                 gukeButton.setBounds(600,487, 72, 29);

                 frame.getContentPane().add(gukeButton);

                

                 JRadioButtonchushiButton = new JRadioButton("\u53A8\u5E08");

                 chushiButton.setBounds(720,487, 72, 29);

                 frame.getContentPane().add(chushiButton);

                

                 JButtonzhuceButton = new JButton("\u6CE8\u518C");

                 zhuceButton.setBackground(Color.ORANGE);

                 zhuceButton.setBounds(575,580, 123, 29);

                 frame.getContentPane().add(zhuceButton);

                 zhuceButton.addActionListener(newActionListener(){

                         publicvoid actionPerformed(ActionEvent a){

                         //TODO Auto-generated method stub

                         Stringyh = yhm.getText(),

                                          psw= mm.getText(),

                                          again= qrmm.getText(),

                                          nc= nicheng.getText();

                         if("".equals(yh)){

                         JOptionPane.showMessageDialog(null,"用户名空了");

                         return;}

                         if(yh.length()<4||yh.length()>20){

                                  JOptionPane.showMessageDialog(null,"用户名不合格,长度应在4~20位之间");

                                  return;}

                         if("".equals(psw)){

                         JOptionPane.showMessageDialog(null,"密码空了");

                         return;}

                         if(psw.length()<6||psw.length()>20){

                         JOptionPane.showMessageDialog(null,"密码不合格,长度应在6~20位之间");

                         return;}

                         if("".equals(again)){

                         JOptionPane.showMessageDialog(null,"确认密码为空");

                         return;}

                         if(!psw.equals(again)){

                         JOptionPane.showMessageDialog(null,"两次密码不一致,请从新输入");

                         return;}

                         if(nc.length()<1||nc.length()>20){

                                  JOptionPane.showMessageDialog(null,"昵称不合格,长度应在1~20位之间");

                                  return;}

                        

                         Stringdriver = "com.mysql.jdbc.Driver";

                String dbName ="NoodleRestaurant";

                String passwrod = "19961225";

                String userName = "root";

                String url ="jdbc:mysql://localhost:3306/" + dbName;

                String sql = "insert into uservalues(?,?,?,?) ";

               

                int status = 0;

                        

                         if(gukeButton.isSelected()) {

                         status = 0;

                     }

                         if(chushiButton.isSelected()) {

                                  status= 1;

                         }

                        

                         try{

                    Class.forName(driver);

                    Connection conn =DriverManager.getConnection(url, userName,

                            passwrod);

                    PreparedStatement ps =conn.prepareStatement(sql);

                    ps.setString(1, yh); 

                    ps.setString(2, nc); 

                    ps.setString(3, psw);

                    ps.setInt(4, status);

                    ps.executeUpdate(); 

                    ps.close(); 

                    conn.close(); 

                   

                    } catch (Exception e) { 

                    System.out.println(e); 

                    } 

                   

                     JOptionPane.showMessageDialog(null,"注册成功!");

                     frame.dispose();

                         }

                        

                 });

                

                 ButtonGroupbtg=new ButtonGroup();

            btg.add(gukeButton);

            btg.add(chushiButton);

                

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\noodle\\zhuce0.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

        }

}

 

dingdanS.java(厨师查看、编辑、删除订单界面)

import java.awt.EventQueue;

 

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Vector;

 

import javax.swing.table.DefaultTableModel;

import javax.swing.JButton;

 

public class dingdanS {

 

        privateJFrame frame;

        privateJTable table;

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          dingdanS window = new dingdanS();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publicdingdanS() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("\u98DF\u9762\u57CB\u4F0F\u00AE\u8BA2\u5355\uFF08\u60A8\u7684\u8EAB\u4EFD\u662F\uFF1A\u53A8\u5E08\uFF09");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 frame.getContentPane().setLayout(null);

                

                 table= new JTable();

                 table.setModel(newDefaultTableModel(

                         newObject[][] {

                         },

                         newString[] {

                                  "\u8BA2\u5355\u53F7","\u987E\u5BA2", "\u603B\u4EF7","\u4E0B\u5355\u65F6\u95F4"

                         }

                 ){

                         Class[]columnTypes = new Class[] {

                                          Integer.class,String.class, Double.class, String.class

                                  };

                                  publicClass getColumnClass(int columnIndex) {

                                          returncolumnTypes[columnIndex];

                                  }

                 });

                 table.setFont(newFont("微软雅黑", Font.PLAIN, 22));

                 table.setBackground(Color.ORANGE);

                 table.setBounds(300,150, 600, 400);

                 table.getColumnModel().getColumn(0).setPreferredWidth(50);

                 table.getColumnModel().getColumn(1).setPreferredWidth(100);

                 table.getColumnModel().getColumn(2).setPreferredWidth(50);

                 table.getColumnModel().getColumn(3).setPreferredWidth(250);

                 table.setVisible(true);

                 frame.getContentPane().add(table);

                

                 DefaultTableModelordert =  (DefaultTableModel) table.getModel();

                

                 JButtonxq = new JButton("\u67E5\u770B\u8BE6\u60C5");

                 xq.setBackground(Color.ORANGE);

                 xq.setBounds(960,150, 110, 29);

                 xq.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  xiangqing.main(null);

                         }

                 });

                 frame.getContentPane().add(xq);

                

                 JButtonwancheng = new JButton("\u8BA2\u5355\u5B8C\u6210");

                 wancheng.setBackground(Color.CYAN);

                 wancheng.setBounds(1100,150, 123, 29);

                 wancheng.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  JOptionPane.showMessageDialog(null,"真棒,订单完成^_^");

                                  frame.dispose();

                                  ddf.main(null);

                         }

                 });

                 frame.getContentPane().add(wancheng);

                

 

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\noodle\\dd.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

                

                 Stringdriver = "com.mysql.jdbc.Driver";

       String dbName = "NoodleRestaurant";

       String passwrod = "19961225";

       String userName = "root";

       String url = "jdbc:mysql://localhost:3306/" + dbName;

       String sql = "select * from orderall";

       

       try {

                         Class.forName(driver);

           Connection conn = DriverManager.getConnection(url, userName,

                    passwrod);

           PreparedStatement ps = conn.prepareStatement(sql);

           

           ResultSet rs = ps.executeQuery(sql);

           while (rs.next()) {

            Vector v = new Vector();

                                  v.add(rs.getInt(1)); 

                         v.add(rs.getString(2));

                         v.add(rs.getDouble(3));

                         v.add(rs.getString(4));

                         ordert.addRow(v);

                         }

           rs.close();

            ps.close();

           

           conn.close();

       }catch (SQLException e) {

                                  System.out.println("MySQL操作错误");

                                  e.printStackTrace();

                         }catch (Exception e) {

                                  e.printStackTrace();

                         }finally {

                                 

                         }

        }

}

 

Home.java(初始主界面)

 

import java.awt.EventQueue;

import javax.swing.*;

 

import java.awt.GridBagLayout;

import java.awt.GridBagConstraints;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Insets;

import java.awt.Color;

 

public class Home {

 

        privateJFrame frame;

        privatedenglu dl = new denglu();

        privatezhuce zc = new zhuce();

 

        /**

         * Launch the application.

         */

        publicstatic void main(String[] args) {

                 EventQueue.invokeLater(newRunnable() {

                         publicvoid run() {

                                  try{

                                          Homewindow = new Home();

                                          window.frame.setVisible(true);

                                  }catch (Exception e) {

                                          e.printStackTrace();

                                  }

                         }

                 });

        }

 

        /**

         * Create the application.

         */

        publicHome() {

                 initialize();

        }

 

        /**

         * Initialize the contents of the frame.

         */

        privatevoid initialize() {

                 frame= new JFrame();

                 frame.setTitle("\u98DF\u9762\u57CB\u4F0F\u00AE\u9910\u996E\u7BA1\u7406\u7CFB\u7EDF");

                 frame.setBounds(100,100, 1280, 780);

                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                

                 JButtonwaimai = new JButton("\u5916\u5356");

                 waimai.setBackground(Color.ORANGE);

                 waimai.setBounds(666,380, 80, 60);

                 waimai.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  JOptionPane.showMessageDialog(null,"请先登录!");

                         }

                 });

                

                 JButtontangchi = new JButton("\u5802\u5403");

                 tangchi.setBackground(Color.ORANGE);

                 tangchi.setBounds(400,380, 80, 60);

                 tangchi.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  JOptionPane.showMessageDialog(null,"请先登录!");

                         }

                 });

                 frame.getContentPane().setLayout(null);

                 frame.getContentPane().add(tangchi);

                

                 JButtondingzuo = new JButton("\u8BA2\u5EA7");

                 dingzuo.setBackground(Color.ORANGE);

                 dingzuo.setBounds(533,380, 80, 60);

                 dingzuo.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  JOptionPane.showMessageDialog(null,"请先登录!");

                         }

                 });

                 frame.getContentPane().add(dingzuo);

                 frame.getContentPane().add(waimai);

                

                 JButtondingdan = new JButton("\u8BA2\u5355");

                 dingdan.setBackground(Color.ORANGE);

                 dingdan.setBounds(800,380, 80, 60);

                 dingdan.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  JOptionPane.showMessageDialog(null,"请先登录!");

                         }

                 });

                 frame.getContentPane().add(dingdan);

                

                 JButtondenglu = new JButton("\u767B\u5F55");

                 denglu.setBackground(Color.CYAN);

                 denglu.setBounds(510,480, 70, 50);

                 denglu.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  dl.main(null);

                                  frame.dispose();

                         }

                 });

                 frame.getContentPane().add(denglu);

                

                 JButtonzhuce = new JButton("\u6CE8\u518C");

                 zhuce.setBounds(700,480, 70, 50);

                 frame.getContentPane().add(zhuce);

                 zhuce.addActionListener(newActionListener() {

                         publicvoid actionPerformed(ActionEvent e) {

                                  zc.main(null);

                         }

                 });

                 zhuce.setBackground(Color.CYAN);

                

                 JPanelpanel = new JPanel();

                 panel.setBounds(0,0, 1280, 780);

                 frame.getContentPane().add(panel);

                 Iconi=new ImageIcon("D:\\PS\\material\\home20.jpg"); /*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/

                 JLabellb = new JLabel(i); //用指定的图片构造标签对象lb

                 frame.getLayeredPane().add(lb,new Integer(Integer.MIN_VALUE));

                 //把标签放在第二层JlayerPane上。

                 lb.setBounds(0,0,i.getIconWidth(),i.getIconHeight());

                 //设置标签的尺寸,即背景图象的大小。

                 panel.setOpaque(false);;

                 panel.add(lb);

        }

}

展开阅读全文

没有更多推荐了,返回首页