• 点菜系统数据库课程设计


    import java.awt.*;
    import java.sql.*;
    import java.awt.event.*;
    import java.util.Vector;
    import javax.swing.*;
    import java.sql.*;
    import javax.swing.border.EmptyBorder;
    public class test1 extends JFrame implements ActionListener {
        //private static final String  = null;
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        test1 frame = new test1();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        int orderid=0;
        public test1() 
        {
            
            setTitle("点菜系统");
            setSize(600,600); //设置窗口大小
            setLocation(100,100); //设置窗口的位置
            
            ImageIcon image = new ImageIcon("Image/huanyingguanglin.PNG"); 
            image.setImage(image.getImage().getScaledInstance(600,530,Image.SCALE_DEFAULT)); 
            // 把背景图片显示在一个标签里面  
            JLabel label = new JLabel(image);  
            // 把标签的大小位置设置为图片刚好填充整个面板  
            label.setBounds(0, 0, this.getWidth(), this.getHeight());  
            // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明  
            JPanel imagePanel = (JPanel) this.getContentPane();  
            imagePanel.setOpaque(false);  
            // 把背景图片添加到分层窗格的最底层作为背景  
            this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); 
            
            JButton jb=new JButton();
            jb.addActionListener(this);  //增加监听
            jb.setActionCommand("Welcome"); //监听信号
            jb.setPreferredSize(new Dimension(250,50));
            jb.setFont(new Font("宋体",Font.BOLD,20));
            jb.setText("欢迎顾客点击进入菜单");
           
            JPanel jp=new JPanel();
            jp.setOpaque(false);
            jp.setLayout(new GridBagLayout());
            GridBagConstraints c=new GridBagConstraints();
            c.fill=GridBagConstraints.NONE;
            c.gridx=0; c.gridy=0;
            c.insets=new Insets(300,0,50,50);
            jp.add(jb,c);
            
            jb=new JButton();
            jb.addActionListener(this);
            jb.setActionCommand("Close");
            jb.setPreferredSize(new Dimension(250,50));
            jb.setFont(new Font("宋体",Font.BOLD,20));
            jb.setText("取消订单退出系统");
            c=new GridBagConstraints();
            c.fill=GridBagConstraints.NONE;
            c.gridx=1; c.gridy=0;
            c.insets=new Insets(300,0,50,50);
            jp.add(jb,c);
            
            this.add(jp);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);  //是否显示    
        }
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                String user = "root" ;   
                String password = "" ;
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn=DriverManager.getConnection(url, user, password);
                PreparedStatement ps=null;
                
                if(e.getActionCommand()=="Welcome")
                {    
                    ps=conn.prepareStatement("select count(*) from OrderDish"); 
                    ResultSet re=ps.executeQuery();
                    while(re.next()) orderid=re.getInt(1);
                    orderid++;
                    ps=conn.prepareStatement("insert into OrderDish values(?,0)");
                    ps.setInt(1, orderid); 
                    ps.executeUpdate();
                    OrderDish order=new OrderDish(this,"菜单",true,orderid); //新的对话界面
                }
                else if(e.getActionCommand()=="Close")
                { 
                    ps=conn.prepareStatement("delete from OrderItem where orderid=?");
                    ps.setInt(1, orderid);
                    ps.executeUpdate();
                    ps=conn.prepareStatement("delete from OrderDish where orderid=?"); 
                    ps.setInt(1, orderid);
                    ps.executeUpdate();
                    this.dispose();
                }    
            }
            catch(Exception exp){ exp.printStackTrace(); }
        }
    }
    
    class OrderDish extends JDialog implements ActionListener
    {
        JPanel[] jp={null,null,null,null};
        JTabbedPane jtp=null;
        JButton but=null;
        JPanel topPane=null,bottomPane=null,pane=null;
        JPanel[] zhushi={null,null,null,null,null,null,null,null,null,null};
        JLabel lab=null;
        
        JCheckBox[] zhushicheck={null,null,null,null,null,null,null,null,null};
        JCheckBox[] sushicheck={null,null,null,null,null,null,null,null};
        JCheckBox[] xiaochicheck={null,null,null,null,null,null};
        JCheckBox[] yinpincheck={null,null,null,null,null,null};
    
        JComboBox[] zhushicombo={null,null,null,null,null,null,null,null,null};
        JComboBox[] sushicombo={null,null,null,null,null,null,null,null};
        JComboBox[] xiaochicombo={null,null,null,null,null,null};
        JComboBox[] yinpincombo={null,null,null,null,null,null};
        
        ImageIcon image=null;
        JScrollPane[] jsp={null,null,null,null};
        
        String[] zhushiAL={ "zhushi0","zhushi1","zhushi2","zhushi3","zhushi4","zhushi5","zhushi6","zhushi7","zhushi8" };
        String[] sushiAL={ "sushi0","sushi1","sushi2","sushi3","sushi4","sushi5","sushi6","sushi7" };
        String[] xiaochiAL={ "xiaochi0","xiaochi1","xiaochi2","xiaochi3","xiaochi4","xiaochi5" };
        String[] yinpinAL={ "yinpin0","yinpin1","yinpin2","yinpin3","yinpin4","yinpin5" };
        
        String[] zhushinumAL={ "zhushinum0","zhushinum1","zhushinum2","zhushinum3","zhushinum4","zhushinum5","zhushinum6","zhushinum7","zhushinum8" };
        String[] sushinumAL={ "sushinum0","sushinum1","sushinum2","sushinum3","sushinum4","sushinum5","sushinum6","sushinum7" };
        String[] xiaochinumAL={ "xiaochinum0","xiaochinum1","xiaochinum2","xiaochinum3","xiaochinum4","xiaochinum5" };
        String[] yinpinnumAL={ "yinpinnum0","yinpinnum1","yinpinnum2","yinpinnum3","yinpinnum4","yinpinnum5" };
        int orderid=0,zhushinum=9,sushinum=8,xiaochinum=6,yinpinnum=6;
        
        OrderDish(Frame a,String b,boolean c,int oid)
        {
            super(a,b,c);
            orderid=oid;
            this.setTitle("菜单");
            this.setSize(600,600);
            this.setLocation(200,100);
            
            
            //右上角碗碟图片
            image = new ImageIcon("Image/碗碟.PNG"); 
            image.setImage(image.getImage().getScaledInstance(300,100,Image.SCALE_DEFAULT)); 
            lab=new JLabel(image);
            //左上角查看当前订单按钮
            but=new JButton();
            but.setForeground(Color.RED);
            but.setFont(new Font("宋体",Font.BOLD,25));
            but.setText("点击查看当前订单");
            but.setBackground(Color.WHITE);
            but.setPreferredSize(new Dimension(280,90));
            but.addActionListener(this);
            but.setActionCommand("ViewOrder"); //点击查看当前订单监听器
            pane=new JPanel();
            pane.setBackground(Color.WHITE);
            pane.add(but);
            
            //上部分面板
            topPane=new JPanel();
            topPane.setLayout(new GridLayout(1,2));
            topPane.add(pane);
            topPane.add(lab);
            this.add(topPane,BorderLayout.NORTH);
            
            //四个选项卡面板
            jp[0]=new JPanel();
            jp[1]=new JPanel();
            jp[2]=new JPanel();
            jp[3]=new JPanel();
    
            //给第0个面板添加内容
            jp[0].setLayout(new GridLayout(3,3,10,10));
            for(int i=0;i<zhushinum;i++) zhushi[i]=new JPanel(); //多个面板存放不同的菜
            
            String[] num={"1","2","3","4","5"};
            String[] zhushiname={"水煮肉片  20元/份 ", "农家小炒肉  19元/份", "回锅牛肉 28元/份",
                    "红烧鱼块 24元/份", "干烧刁子鱼 24元/份", "干锅手撕鸡 22元/份",
                    "香菇焖鸡 26元/份", "四季豆牛肚丝 26元/份", "鱼香肉丝 19元/份"};
            String[] zhushitp={
                   "Image/shuizhuroupian.PNG","Image/nongjiaxiaochaorou.PNG","Image/huiguoniurou.PNG",
                   "Image/hongshaoyukuai.PNG","Image/ganshaodiaoziyu.PNG","Image/ganguoshousiji.PNG",
                   "Image/xianggumenji.PNG","Image/sijidouniudusi.PNG","Image/yuxiangrousi.PNG"
            };
            //给每个菜的面板添加内容
            for(int i=0;i<zhushinum;i++)
            {
                bottomPane=new JPanel();
                bottomPane.setLayout(new GridLayout(2,1));
                
                zhushicheck[i]=new JCheckBox("选中");
                zhushicheck[i].addActionListener(this);
                zhushicheck[i].setActionCommand(zhushiAL[i]); //增加监听器
                
                lab=new JLabel(zhushiname[i]);
                bottomPane.add(lab); 
                
                zhushicombo[i]=new JComboBox(num); //下拉列表框
                zhushicombo[i].setSelectedIndex(0); //设置默认为1(第0个数)
                zhushicombo[i].addActionListener(this);
                zhushicombo[i].setActionCommand(zhushinumAL[i]);
                
                lab=new JLabel("份");
                pane=new JPanel();
                pane.add(zhushicheck[i]); pane.add(zhushicombo[i]); pane.add(lab);
                bottomPane.add(pane); 
                
                image= new ImageIcon(zhushitp[i]); 
                image.setImage(image.getImage().getScaledInstance(180,120,Image.SCALE_DEFAULT));
                lab=new JLabel(image);
                
                zhushi[i].add(lab,BorderLayout.CENTER);
                zhushi[i].add(bottomPane,BorderLayout.SOUTH);
            }
            
            //把所有菜的面板加入到大面板中去
            for(int i=0;i<zhushinum;i++) jp[0].add(zhushi[i]);
            jp[0].setPreferredSize(new Dimension(560,600));
            jsp[0]=new JScrollPane(jp[0]);
         
            //给第1个面板添加内容
            String[] sushiname={
               "酸辣土豆丝 12元/份","番茄炒蛋 13元/份","豆角炒茄子 13元/份",
               "麻婆豆腐 12元/份","手撕包菜 10元/份","小炒香干 13元/份",
               "小炒千叶豆腐 16元/份","口味黑木耳 16元/份"
            };
            String[] sushitp={
                "sushi/suanlatudousi.PNG","sushi/fanqiechaodan.PNG","sushi/doujiaochaoqiezi.PNG",
                "sushi/mapodoufu.PNG","sushi/shousibaocai.PNG","sushi/xiaochaoxianggan.PNG",
                "sushi/xiaochaoqianyedoufu.PNG","sushi/kouweiheimuer.PNG"
            };
            jp[1].setLayout(new GridLayout(3,3,10,10));
            for(int i=0;i<sushinum;i++) zhushi[i]=new JPanel(); //多个面板存放不同的菜
        
            //给每个菜的面板添加内容
            for(int i=0;i<sushinum;i++)
            {
                bottomPane=new JPanel();
                bottomPane.setLayout(new GridLayout(2,1));
                
                sushicheck[i]=new JCheckBox("选中");
                sushicheck[i].addActionListener(this);
                sushicheck[i].setActionCommand(sushiAL[i]); //增加监听器
                
                lab=new JLabel(sushiname[i]);
                bottomPane.add(lab); 
                
                sushicombo[i]=new JComboBox(num); //下拉列表框
                sushicombo[i].setSelectedIndex(0); //设置默认为1(第0个数)
                sushicombo[i].addActionListener(this);
                sushicombo[i].setActionCommand(sushinumAL[i]);
                
                lab=new JLabel("份");
                pane=new JPanel();
                pane.add(sushicheck[i]); pane.add(sushicombo[i]); pane.add(lab);
                bottomPane.add(pane); 
                
                image= new ImageIcon(sushitp[i]); 
                image.setImage(image.getImage().getScaledInstance(180,120,Image.SCALE_DEFAULT));
                lab=new JLabel(image);
                
                zhushi[i].add(lab,BorderLayout.CENTER);
                zhushi[i].add(bottomPane,BorderLayout.SOUTH);
            }
            for(int i=0;i<sushinum;i++) jp[1].add(zhushi[i]);
            jp[1].setPreferredSize(new Dimension(560,600));
            jsp[1]=new JScrollPane(jp[1]);
            
            //给第2个面板添加内容
            String[] xiaochiname={
                "鸡米花  10元/份","无骨鸡柳 10元/份","骨肉相连 10元/份",
                "薯条 8元/份","川香鸡柳 10元/份","香酥鸡块 15元/份"
            };
            String[] xiaochitp={
                    "xiaochi/jimihua.PNG","xiaochi/wugujiliu.PNG","xiaochi/gurouxianglian.PNG",
                    "xiaochi/shutiao.PNG","xiaochi/chuanxiangjiliu.PNG","xiaochi/xiangsujikuai.PNG"
            };
            jp[2].setLayout(new GridLayout(2,3,10,10));
            for(int i=0;i<xiaochinum;i++) zhushi[i]=new JPanel(); //多个面板存放不同的菜
        
            //给每个菜的面板添加内容
            for(int i=0;i<xiaochinum;i++)
            {
                bottomPane=new JPanel();
                bottomPane.setLayout(new GridLayout(2,1));
                
                xiaochicheck[i]=new JCheckBox("选中");
                xiaochicheck[i].addActionListener(this);
                xiaochicheck[i].setActionCommand(xiaochiAL[i]); //增加监听器
                
                lab=new JLabel(xiaochiname[i]);
                bottomPane.add(lab); 
                
                xiaochicombo[i]=new JComboBox(num); //下拉列表框
                xiaochicombo[i].setSelectedIndex(0); //设置默认为1(第0个数)
                xiaochicombo[i].addActionListener(this);
                xiaochicombo[i].setActionCommand(xiaochinumAL[i]);
                
                lab=new JLabel("份");
                pane=new JPanel();
                pane.add(xiaochicheck[i]); pane.add(xiaochicombo[i]); pane.add(lab);
                bottomPane.add(pane); 
                
                image= new ImageIcon(xiaochitp[i]); 
                image.setImage(image.getImage().getScaledInstance(180,120,Image.SCALE_DEFAULT));
                lab=new JLabel(image);
                
                zhushi[i].add(lab,BorderLayout.CENTER);
                zhushi[i].add(bottomPane,BorderLayout.SOUTH);
            }
            for(int i=0;i<xiaochinum;i++) jp[2].add(zhushi[i]);
            jp[2].setPreferredSize(new Dimension(560,400));
            jsp[2]=new JScrollPane(jp[2]);
            
            //给第3个面板添加内容
            String[] yinpinname={
                    "红豆奶茶  10元/份","芒果奶昔 10元/份","蜜桃奶昔 10元/份",
                    "姜母茶 15元/份","原味奶茶 10元/份","咖啡奶茶 10元/份"
                };
            String[] yinpintp={
                "yinpin/hongdounaicha.PNG","yinpin/mangguonaixi.PNG","yinpin/mitaonaixi.PNG",
                "yinpin/jiangmucha.PNG","yinpin/yuanweinaicha.PNG","yinpin/kafeinaicha.PNG"
            };
                jp[3].setLayout(new GridLayout(2,3,10,10));
                for(int i=0;i<yinpinnum;i++) zhushi[i]=new JPanel(); //多个面板存放不同的菜
            
                //给每个菜的面板添加内容
                for(int i=0;i<yinpinnum;i++)
                {
                    bottomPane=new JPanel();
                    bottomPane.setLayout(new GridLayout(2,1));
                    
                    yinpincheck[i]=new JCheckBox("选中");
                    yinpincheck[i].addActionListener(this);
                    yinpincheck[i].setActionCommand(yinpinAL[i]); //增加监听器
                    
                    lab=new JLabel(yinpinname[i]);
                    bottomPane.add(lab); 
                    
                    yinpincombo[i]=new JComboBox(num); //下拉列表框
                    yinpincombo[i].setSelectedIndex(0); //设置默认为1(第0个数)
                    yinpincombo[i].addActionListener(this);
                    yinpincombo[i].setActionCommand(yinpinnumAL[i]);
                    
                    lab=new JLabel("份");
                    pane=new JPanel();
                    pane.add(yinpincheck[i]); pane.add(yinpincombo[i]); pane.add(lab);
                    bottomPane.add(pane); 
                    
                    image= new ImageIcon(yinpintp[i]); 
                    image.setImage(image.getImage().getScaledInstance(180,120,Image.SCALE_DEFAULT));
                    lab=new JLabel(image);
                    
                    zhushi[i].add(lab,BorderLayout.CENTER);
                    zhushi[i].add(bottomPane,BorderLayout.SOUTH);
                }
            
            //把所有菜的面板加入到大面板中去
            for(int i=0;i<yinpinnum;i++) jp[3].add(zhushi[i]);
            jp[3].setPreferredSize(new Dimension(560,400));
            jsp[3]=new JScrollPane(jp[3]);
            
            jtp=new JTabbedPane();
            jtp.add("主食",jsp[0]);
            jtp.add("素食",jsp[1]);
            jtp.add("小吃",jsp[2]);
            jtp.add("饮品",jsp[3]);
            
            
            
            this.add(jtp,BorderLayout.CENTER);
            
            JPanel bottomPane=new JPanel();
            but=new JButton();
            but.setFont(new Font("宋体",Font.BOLD,20));
            but.setText(" 前往结账   ");
            but.addActionListener(this);
            but.setActionCommand("jiezhang");
            bottomPane.add(but);
            
            but=new JButton();
            but.setFont(new Font("宋体",Font.BOLD,20));
            but.setText(" 重新选菜   ");
            but.addActionListener(this);
            but.setActionCommand("chongxuan");
            bottomPane.add(but);
            
            but=new JButton();
            but.setFont(new Font("宋体",Font.BOLD,20));
            but.setText("   返回     ");
            but.addActionListener(this);
            but.setActionCommand("Close");
            bottomPane.add(but);
            
            this.add(bottomPane,BorderLayout.SOUTH);
            
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setVisible(true);  //是否显示    
        }
        public int Getzhushi(String s)
        {
            for(int i=0;i<zhushinum;i++) if(s==zhushiAL[i]) return i;
            return -1;
        }
        public int Getzhushinum(String s)
        {
            for(int i=0;i<zhushinum;i++) if(s==zhushinumAL[i]) return i;
            return -1;
        }
        public int Getsushi(String s)
        {
            for(int i=0;i<sushinum;i++) if(s==sushiAL[i]) return i;
            return -1;
        }
        public int Getsushinum(String s)
        {
            for(int i=0;i<sushinum;i++) if(s==sushinumAL[i]) return i;
            return -1;
        }
        public int Getxiaochi(String s)
        {
            for(int i=0;i<xiaochinum;i++) if(s==xiaochiAL[i]) return i;
            return -1;
        }
        public int Getxiaochinum(String s)
        {
            for(int i=0;i<xiaochinum;i++) if(s==xiaochinumAL[i]) return i;
            return -1;
        }
        public int Getyinpin(String s)
        {
            for(int i=0;i<yinpinnum;i++) if(s==yinpinAL[i]) return i;
            return -1;
        }
        public int Getyinpinnum(String s)
        {
            for(int i=0;i<yinpinnum;i++) if(s==yinpinnumAL[i]) return i;
            return -1;
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand()=="Close") this.dispose(); //关闭窗口
            else if(e.getActionCommand()=="jiezhang") { Check ck=new Check(this,"结账",true,orderid); }//产生结账窗口
            else if(e.getActionCommand()=="chongxuan")
            {
                //把所有的选择取消,并删除数据库中所有选菜内容
                for(int i=0;i<zhushinum;i++) 
                { 
                    if(zhushicheck[i].isSelected())    zhushicheck[i].setSelected(false);
                    zhushicombo[i].setSelectedIndex(0);
                }
                for(int i=0;i<sushinum;i++) 
                {
                     if(sushicheck[i].isSelected()) sushicheck[i].setSelected(false);
                     zhushicombo[i].setSelectedIndex(0);
                }
                for(int i=0;i<xiaochinum;i++) 
                {
                    if(xiaochicheck[i].isSelected()) xiaochicheck[i].setSelected(false);
                    xiaochicombo[i].setSelectedIndex(0);
                }
                for(int i=0;i<yinpinnum;i++)
                {
                    if(yinpincheck[i].isSelected()) yinpincheck[i].setSelected(false);
                    yinpincombo[i].setSelectedIndex(0);
                }
                try
                {
                    String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                    String user = "root" ;   
                    String password = "" ;
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn=DriverManager.getConnection(url, user, password);
                    PreparedStatement ps=null; 
                    
                    ps=conn.prepareStatement("delete from OrderItem where orderid=?"); 
                    ps.setInt(1, orderid);
                    ps.executeUpdate();        
                }
                catch(Exception exp)
                {
                     exp.printStackTrace();
                }
                return;
            }
            else if(e.getActionCommand()=="ViewOrder"){ View view=new View(this,"当前订单",true,orderid); }
            else
            {
                try
                {
                    String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                    String user = "root" ;   
                    String password = "" ;
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn=DriverManager.getConnection(url, user, password);
    
                    PreparedStatement ps=null; 
                    int a=Getzhushi(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(zhushicheck[a].isSelected()) 
                        {
                            ps=conn.prepareStatement("insert into OrderItem values(?,?,?)");
                            ps.setInt(1,orderid); ps.setString(2, zhushiAL[a]);
                            int num=zhushicombo[a].getSelectedIndex()+1;
                            ps.setInt(3, num);
                            ps.executeUpdate();
                        }
                        else
                        {
                            ps=conn.prepareStatement("delete from OrderItem where orderid=? and id=?");
                            ps.setInt(1, orderid); ps.setString(2, zhushiAL[a]);
                            ps.executeUpdate();
                        }
                        return;
                    }
                    a=Getsushi(e.getActionCommand());
                    if(a!=-1)
                    {
                        
                        if(sushicheck[a].isSelected()) 
                        {
                            ps=conn.prepareStatement("insert into OrderItem values(?,?,?)");
                            ps.setInt(1,orderid); ps.setString(2, sushiAL[a]);
                            int num=sushicombo[a].getSelectedIndex()+1;
                            ps.setInt(3, num);
                            ps.executeUpdate();
                        }
                        else
                        {
                            ps=conn.prepareStatement("delete from OrderItem where orderid=? and id=?");
                            ps.setInt(1, orderid); ps.setString(2, sushiAL[a]);
                            ps.executeUpdate();
                        }
                        return;
                    }
                    a=Getxiaochi(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(xiaochicheck[a].isSelected()) 
                        {
                            ps=conn.prepareStatement("insert into OrderItem values(?,?,?)");
                            ps.setInt(1,orderid); ps.setString(2, xiaochiAL[a]);
                            int num=xiaochicombo[a].getSelectedIndex()+1;
                            ps.setInt(3, num);
                            ps.executeUpdate();
                        }
                        else
                        {
                            ps=conn.prepareStatement("delete from OrderItem where orderid=? and id=?");
                            ps.setInt(1, orderid); ps.setString(2, xiaochiAL[a]);
                            ps.executeUpdate();
                        }
                        return;
                    }
                    a=Getyinpin(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(yinpincheck[a].isSelected()) 
                        {
                            ps=conn.prepareStatement("insert into OrderItem values(?,?,?)");
                            ps.setInt(1,orderid); ps.setString(2, yinpinAL[a]);
                            int num=yinpincombo[a].getSelectedIndex()+1;
                            ps.setInt(3, num);
                            ps.executeUpdate();
                        }
                        else
                        {
                            ps=conn.prepareStatement("delete from OrderItem where orderid=? and id=?");
                            ps.setInt(1, orderid); ps.setString(2, yinpinAL[a]);
                            ps.executeUpdate();
                        }
                        return;
                    }
                    a=Getzhushinum(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(zhushicheck[a].isSelected())
                        {
                            ps=conn.prepareStatement("update OrderItem set num=? where orderid=? and id=?");
                            int num=zhushicombo[a].getSelectedIndex()+1;
                            ps.setInt(1, num);    ps.setInt(2, orderid); ps.setString(3, zhushiAL[a]);    
                            ps.executeUpdate();
                        }
                    }
                    a=Getsushinum(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(sushicheck[a].isSelected())
                        {
                            ps=conn.prepareStatement("update OrderItem set num=? where orderid=? and id=?");
                            int num=sushicombo[a].getSelectedIndex()+1;
                            ps.setInt(1, num);    ps.setInt(2, orderid); ps.setString(3, sushiAL[a]);    
                            ps.executeUpdate();
                        }
                    }
                    a=Getxiaochinum(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(xiaochicheck[a].isSelected())
                        {
                            ps=conn.prepareStatement("update OrderItem set num=? where orderid=? and id=?");
                            int num=xiaochicombo[a].getSelectedIndex()+1;
                            ps.setInt(1, num);    ps.setInt(2, orderid); ps.setString(3, xiaochiAL[a]);    
                            ps.executeUpdate();
                        }
                    }
                    a=Getyinpinnum(e.getActionCommand());
                    if(a!=-1)
                    {
                        if(yinpincheck[a].isSelected())
                        {
                            ps=conn.prepareStatement("update OrderItem set num=? where orderid=? and id=?");
                            int num=yinpincombo[a].getSelectedIndex()+1;
                            ps.setInt(1, num);    ps.setInt(2, orderid); ps.setString(3, yinpinAL[a]);    
                            ps.executeUpdate();
                        }
                    } 
                    
                }
                catch(Exception exp)
                {
                    exp.printStackTrace();
                }
                
            }
        }
    }
    
    class View extends JDialog implements ActionListener
    {
        JScrollPane[] jsp={null,null,null,null,null};
        int orderid=0;
        View(JDialog a,String b,boolean c,int oid)
        {
            super(a,b,c);
            orderid=oid;
            this.setSize(600,600);
            this.setLocation(300,100);
            //连接数据库把选菜信息显示出来
            JTabbedPane jtp=new JTabbedPane();
            String[] sql={
                "select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id",
                "select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id and Dish.id like 'zhushi%'",
                "select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id and Dish.id like 'sushi%'",
                "select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id and Dish.id like 'xiaochi%'",
                "select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id and Dish.id like 'yinpin%'"
            };
            try
            {
                String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                String user = "root" ;   
                String password = "" ;
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn=DriverManager.getConnection(url, user, password);
                PreparedStatement ps=null; 
                
                for(int i=0;i<5;i++)
                {
                    Vector ziduan=new Vector();
                    ziduan.add("菜名"); ziduan.add("单价"); ziduan.add("数量");
                    Vector jilu=new Vector(); //集合类      
                    //这里就要连接数据库把信息显示出来
                    ps=conn.prepareStatement(sql[i]); 
                    ps.setInt(1, orderid);
                    ResultSet re=ps.executeQuery();
                    while(re.next())
                    {
                        String name=re.getString(1);
                        String price =String.format("%d", re.getInt(2));
                        String num=String.format("%d", re.getInt(3));
                        Vector v=new Vector();
                        v.add(name); v.add(price); v.add(num);
                        jilu.add(v);
                    }
                    JTable tab=new JTable(jilu,ziduan);//JTable是表格,里面的参数是先记录后字段
                    tab.setFont(new Font("楷体",Font.BOLD,20));
                    tab.setBackground(Color.CYAN);
                    tab.setRowHeight(30);
                    jsp[i]=new JScrollPane(tab); //滚动条
                    
                }
            }
            catch(Exception exp)
            {
                 exp.printStackTrace();
            }
            
            jtp.add("所有",jsp[0]);
            jtp.add("主食",jsp[1]);
            jtp.add("素食",jsp[2]);
            jtp.add("小吃",jsp[3]);
            jtp.add("饮品",jsp[4]);
            this.add(jtp,BorderLayout.CENTER);
            
            JButton but=new JButton();
            but.setFont(new Font("宋体",Font.BOLD,20));
            but.setText(" 返回  ");
            but.addActionListener(this);
            but.setActionCommand("exit");
            JPanel pane=new JPanel();
            pane.add(but,BorderLayout.CENTER);
            this.add(pane,BorderLayout.SOUTH);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setVisible(true);  //是否显示
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand()=="exit")
            {
                this.dispose();
            }
        }
    }
    class Check extends JDialog implements ActionListener
    {
        JButton[] jb={null,null};
        int orderid=0,sum=0;
        Check(JDialog a,String b,boolean c,int oid)
        {
            super(a,b,c);
            orderid=oid;
            this.setTitle("结账");
            this.setSize(600,600);
            this.setLocation(300,100);
            
            //连接数据库把所有选菜信息显示出来
            Vector ziduan=new Vector();
            ziduan.add("菜名"); ziduan.add("单价"); ziduan.add("数量");
            Vector jilu=new Vector();
            //连接数据库得到信息
            try
            {
                String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                String user = "root" ;   
                String password = "" ;
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn=DriverManager.getConnection(url, user, password);
                PreparedStatement ps=null; 
                
                ps=conn.prepareStatement("select name,price,num from Dish,OrderItem where orderid=? and OrderItem.id=Dish.id"); 
                ps.setInt(1, orderid);
                ResultSet re=ps.executeQuery();
                while(re.next())
                {
                    String name=re.getString(1);
                    int p=re.getInt(2);
                    int t=re.getInt(3);
                    sum+=p*t;
                    String price =String.format("%d", p);
                    String num=String.format("%d", t);
                    Vector v=new Vector();
                    v.add(name); v.add(price); v.add(num);
                    jilu.add(v);
                }
                
            }
            catch(Exception exp)
            {
                 exp.printStackTrace();
            }
            //加总价标签
            JLabel lab=new JLabel();
            lab.setFont(new Font("宋体",Font.BOLD,20));
            lab.setText("总计 "+sum+"元");
            lab.setHorizontalAlignment(JLabel.CENTER);
            this.add(lab,BorderLayout.NORTH);
            //加表格记录
            JTable tab=new JTable(jilu,ziduan);//JTable是表格,里面的参数是先记录后字段
            tab.setFont(new Font("楷体",Font.BOLD,20));
            tab.setBackground(Color.CYAN);
            tab.setRowHeight(30);
            JScrollPane jsp=new JScrollPane(tab); //滚动条    
            this.add(jsp,BorderLayout.CENTER);
            //加按钮
            jb[0]=new JButton();
            jb[0].setFont(new Font("宋体",Font.BOLD,20));
            jb[0].setText("  确认支付   ");
            jb[0].addActionListener(this);
            jb[0].setActionCommand("Pay");
            
            jb[1]=new JButton();
            jb[1].setFont(new Font("宋体",Font.BOLD,20));
            jb[1].setText("  返回    ");
            jb[1].addActionListener(this);
            jb[1].setActionCommand("Close");
            
            JPanel bottomPane=new JPanel();
            bottomPane.add(jb[0]); bottomPane.add(jb[1]);
            this.add(bottomPane,BorderLayout.SOUTH);
            
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setVisible(true);  //是否显示
        }
        public void actionPerformed(ActionEvent e)
        {
            //
            if(e.getActionCommand()=="Close")
            {
                this.dispose();
            }
            else if(e.getActionCommand()=="Pay")
            {
                try
                {
                    String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
                    String user = "root" ;   
                    String password = "" ;
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn=DriverManager.getConnection(url, user, password);
    
                    PreparedStatement ps=null; 
                    ps=conn.prepareStatement("update OrderDish set money=? where orderid=?");
                    ps.setInt(1, sum); ps.setInt(2, orderid);
                    ps.executeUpdate();                
                }
                catch(Exception exp)
                {
                     exp.printStackTrace();
                }
                Pay pay=new Pay(this,"支付",true);
            }
        }
    }
    
    class Pay extends JDialog implements ActionListener
    {
        JTabbedPane jtp=null;
        JButton jb=null;
        JPanel bottomPane=null;
        JLabel[] jl={null,null,null,null};
        ImageIcon image=null;
        Pay(JDialog a,String b,boolean c)
        {
            super(a,b,c);
            this.setSize(600,600);
            this.setLocation(400,100);
            image= new ImageIcon("Image/zhifubao.PNG"); 
            image.setImage(image.getImage().getScaledInstance(600,500,Image.SCALE_DEFAULT));
            jl[0]=new JLabel(image);
            image= new ImageIcon("Image/weixin.PNG"); 
            image.setImage(image.getImage().getScaledInstance(600,500,Image.SCALE_DEFAULT));
            jl[1]=new JLabel(image);
            image= new ImageIcon("Image/wangyin.PNG"); 
            image.setImage(image.getImage().getScaledInstance(600,500,Image.SCALE_DEFAULT));
            jl[2]=new JLabel(image);
            image= new ImageIcon("Image/xianjing.PNG"); 
            image.setImage(image.getImage().getScaledInstance(600,500,Image.SCALE_DEFAULT));
            jl[3]=new JLabel(image);
            
            jtp=new JTabbedPane();
            jtp.add("支付宝支付",jl[0]);
            jtp.add(" 微信支付  ",jl[1]);
            jtp.add(" 网银支付  ",jl[2]);
            jtp.add(" 现金支付  ",jl[3]);
            
            this.add(jtp,BorderLayout.CENTER);
            
            jb=new JButton();
            jb.setFont(new Font("宋体",Font.BOLD,20));
            jb.setText("   完成退出      ");
            jb.addActionListener(this);
            jb.setActionCommand("exit");
            
            bottomPane=new JPanel();
            bottomPane.add(jb);
            this.add(bottomPane,BorderLayout.SOUTH);
            
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setVisible(true);  
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand()=="exit")
            {
                this.dispose();
            }
        }
    }
    View Code

    需要添加n多图片,加载连接mysql的驱动器

    数据库建表

    CREATE TABLE OrderDish
    (
        orderid int not null,
        money int,
        primary key(orderid)
    );
    
    
    CREATE TABLE Dish
    (
        id varchar(20) not null,
        name nchar(10),
        price int,
        type nchar(10),
        primary key(id)
    );
    insert into Dish values('zhushi0','水煮肉片',20,'主食');
    insert into Dish values('zhushi1','农家小炒肉',19,'主食');
    insert into Dish values('zhushi2','回锅牛肉',28,'主食');
    insert into Dish values('zhushi3','红烧鱼块',24,'主食');
    insert into Dish values('zhushi4','干烧刁子鱼',24,'主食');
    insert into Dish values('zhushi5','干锅手撕鸡',22,'主食');
    insert into Dish values('zhushi6','香菇焖鸡',26,'主食');
    insert into Dish values('zhushi7','四季豆牛肚丝',26,'主食');
    insert into Dish values('zhushi8','鱼香肉丝',19,'主食');  
      
    insert into Dish values('sushi0','酸辣土豆丝',12,'素食');
    insert into Dish values('sushi1','番茄炒蛋',13,'素食');
    insert into Dish values('sushi2','豆角炒茄子',13,'素食');
    insert into Dish values('sushi3','麻婆豆腐',12,'素食');
    insert into Dish values('sushi4','手撕包菜',10,'素食');
    insert into Dish values('sushi5','小炒香干',13,'素食');
    insert into Dish values('sushi6','小炒千叶豆腐',16,'素食');
    insert into Dish values('sushi7','口味黑木耳',16,'素食');
    
    insert into Dish values('xiaochi0','鸡米花',10,'小吃');
    insert into Dish values('xiaochi1','无骨鸡柳',10,'小吃');
    insert into Dish values('xiaochi2','骨肉相连',10,'小吃');
    insert into Dish values('xiaochi3','薯条',8,'小吃');
    insert into Dish values('xiaochi4','川香鸡柳',10,'小吃');
    insert into Dish values('xiaochi5','香酥鸡块',15,'小吃');
    
    insert into Dish values('yinpin0','红豆奶茶',10,'饮品');
    insert into Dish values('yinpin1','芒果奶昔',10,'饮品');
    insert into Dish values('yinpin2','蜜桃奶昔',10,'饮品');
    insert into Dish values('yinpin3','姜母茶',15,'饮品');
    insert into Dish values('yinpin4','原味奶茶',10,'饮品');
    insert into Dish values('yinpin5','咖啡奶茶',10,'饮品');
    
    CREATE TABLE OrderItem
    (
        orderid int not null,
        id varchar(20) not null,
        num int,
        primary key(orderid,id),
        foreign key(orderid) references OrderDish(orderid),
        foreign key(id) references Dish(id)
    );
    
    drop table OrderItem;
    drop table Dish;
    drop table OrderDish;
    
    
    try
    {
                        
    }
    catch(Exception exp)
    {
         exp.printStackTrace();
    }
    
    String url = "jdbc:mysql://localhost:3306/test" ;  //下面几行都是模板    
    String user = "root" ;   
    String password = "" ;
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection(url, user, password);
    
    PreparedStatement ps=null; 
    ps=conn.prepareStatement("select count(*) from OrderDish"); 
    ResultSet re=ps.executeQuery();
    View Code

    效果图

      

    小结

    完成这个课设花了一个星期的时间,独立设计,实现,调bug,调整界面,希望能把界面做得丰富一些。在数据库查询时,查询的条件会发生多次变化,所以使用了prepareStatement,只需要设置相关参数就可以复用。

  • 相关阅读:
    json针对list map set 应用
    数据转换与枚举
    override和overload的区别
    JAVA NIO 基础学习
    JavaScript ES6箭头函数指南
    Javascript 中的神器
    如何编写Hexo主题
    什么是闭包?闭包的优缺点?
    微信小程序封装http访问网络库实例代码
    微信小程序 生命周期函数详解
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/5894264.html
Copyright © 2020-2023  润新知