Java-Java基础11之GUI与网络编程的应用开发餐饮管理系统
主要内容有:Java基础之GUI编程与网络编程的应用、实现一个小型的餐饮管理系统。
文件结构
满汉楼餐饮管理系统_界面及布局
人事JPanel界面布局
主界面布局
闪屏
登录界面
主界面
人事管理界面
数据库表设计
用户登录表与用户信息表
--run:
--sqlcmd -U sa -P luowei -d master
CREATE DATABASE MyRestaurantDB
ON PRIMARY
(NAME ='data_1',
FILENAME ='D:\Workspace\MyEclipse 8.5\满汉楼项目开发\data\data_1.mdf',
SIZE =3 MB, MAXSIZE =200 MB,
FILEGROWTH =500 KB),
FILEGROUP data2
(NAME ='data_2',
FILENAME ='D:\Workspace\MyEclipse 8.5\满汉楼项目开发\data\data_2.ndf',
SIZE =1 MB, MAXSIZE =100 MB,
FILEGROWTH =500 KB)
LOG ON
(NAME ='log_1',
FILENAME ='D:\Workspace\MyEclipse 8.5\满汉楼项目开发\data\log_1.ldf',
SIZE =1 MB, MAXSIZE =100 MB,
FILEGROWTH =500 KB)
GO
USE MyRestaurantDB
GO
--创建登陆用户的信息表
CREATE TABLE clerkInfo
(
cleId NVARCHAR(30) PRIMARY KEY NOT NULL,
cleName NVARCHAR(30) NOT NULL,
cleSex NCHAR(1) CHECK (cleSex IN ('男', '女')) DEFAULT '女' NOT NULL,
cleAge INT CHECK (cleAge > 1) NOT NULL,
cleZw NVARCHAR(30) NOT NULL
)
GO
--创建用户登陆
CREATE TABLE login
(
cleId NVARCHAR(30) FOREIGN KEY REFERENCES clerkInfo (cleId),
passwd NVARCHAR(30) NOT NULL
)
GO
INSERT INTO clerkInfo VALUES ('m001', '罗维', '男', 22, '主管')
INSERT INTO clerkInfo VALUES ('m002', '黄蓉', '女', 23, '管理员')
INSERT INTO clerkInfo VALUES ('m003', '林冲', '男', 28, '经理')
INSERT INTO clerkInfo VALUES ('m004', '西施', '女', 30, '会计')
INSERT INTO clerkInfo VALUES ('m005', '貂蝉', '女', 24, '收银员')
INSERT INTO clerkInfo VALUES ('m006', '王昭君', '女', 26, '服务员')
INSERT INTO clerkInfo VALUES ('m007', '陈花', '女', 28, '服务员')
INSERT INTO clerkInfo VALUES ('m008', '武松', '男', 25, '经理')
GO
INSERT INTO login VALUES ('m001', 'mhl001')
INSERT INTO login VALUES ('m002', 'mhl002')
INSERT INTO login VALUES ('m003', 'mhl003')
GO
SELECT *
FROM clerkInfo
SELECT *
FROM login
SELECT
l.cleId,
l.passwd,
c.cleName,
c.cleZw
FROM clerkInfo c, login l
WHERE c.cleId = l.cleId
ORDER BY cleAge
--drop database MyRestaurantDB
--drop table login
--drop table clerkInfo
界面_com.mhl.view/Index.java
/*
* 用户登录的闪屏效果
*/
package com.mhl.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Index extends JWindow implements Runnable{
paint p;
public static void main(String[] args) {
//
Index index=new Index();
Thread t=new Thread(index);
t.start();
}
public Index()
{
//创建p
p=new paint();
this.add(p);
this.setSize(400,250);
//确定JWindow的初始位置
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width/2-200,height/2-150);
this.setVisible(true);
}
@Override
public void run() {
while(true)
{
//等闪屏效果结束,跳转到用户登录界面
try {
Thread.sleep(30*500);
} catch (InterruptedException e) {
//
e.printStackTrace();
}
//跳转到登录界面
System.out.println("闪屏结束");
new UserLogin();
//同时让闪屏消失
this.dispose();
break;//退出线程
}
}
}
//开发一个闪屏类
class paint extends JPanel implements Runnable
{
Thread t;
int x=10;
int i=0,j=40,u=10;
String gg[]={"系","统","正","在","加","载","请","稍","后"};
int k=0,tt=0;
String shi[]={"满","汉","楼","融","满","汉","精","华","做","天","下","美","味",
"招","八","方","食","客","结","四","海","良","朋","|","满","汉","楼","工","作","室"};
Font f=new Font("隶书",Font.PLAIN,18);
boolean ifok=true;
int width=180;
int height=0;
int dian=0;
paint()
{
t=new Thread(this);
t.start();
}
@Override
public void run() {
while(true)
{
if(x<=380) repaint();
try {
Thread.sleep(100);
i++;
j=j-6;
u=u+10;
if(tt==3) width=width-20;
if(i==4)
{
tt++;
if(ifok&&x>120+k*20) k++;
if(k>=gg.length-1) ifok=false;
x=x+10;
i=0;
j=40;
u=10;
dian++;
if(dian>3) dian=0;
}
} catch (InterruptedException e) {
System.out.println("线程中断");
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g)
{
Image image;
image=Toolkit.getDefaultToolkit().getImage("image/index/index.gif");
g.drawImage(image, 0, 0, this.getWidth(), 200, this);
int r=(int)(Math.random()*255);
int b=(int)(Math.random()*255);
int y=(int)(Math.random()*255);
g.setColor(new Color(253,250,250));
g.fillRect(x, 210, 390-x, 30);
g.setColor(new Color(253,250,250));
if(i>1) g.fillRect(x, 225-(j+20)/2, 10, j+20);
if(j>25) g.setColor(new Color(r,b,y));
else g.setColor(new Color(123,194,252));
g.fillRect(x, 225-j/2, 10, j);
g.setColor(new Color(123,194,252));
g.drawRect(10, 210, 380, 30);
if(x<120)
{
for(int l=0;l<gg.length;l++)
{
g.setColor(new Color(0,0,0));
g.drawString(gg[l],120+l*20,230);
}
for(int l=0;l<dian;l++)
{
g.setColor(new Color(0,0,0));
g.drawString("*", 300+l*10, 235);
}
g.drawString("*", 300+10*dian, 235);
}
else
{
g.setColor(new Color(23,23,230));
g.drawString(gg[k],120+k*20,230);
for(int l=k+1;l<gg.length;l++)
{
g.setColor(new Color(0,0,0));
g.drawString(gg[l],120+l*20,230);
}
if(x>300+dian*10)
g.setColor(new Color(23,23,230));
for(int l=0;l<dian;l++)
{
g.drawString("*", 300+l*10, 235);
}
g.drawString("*",300+10*dian,235);
}
//-----------逐字写诗
if(tt<3)
{
for(int rr=0;rr<=tt;rr++)
{
g.setColor(new Color(r,b,y));
g.drawString(shi[rr], 170, 60+rr*20);
}
g.drawString(shi[tt], 170, 60+tt*20);
}
if(tt>=3&&tt<7)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
g.setColor(new Color(r,b,y));
if(tt<8)
for(int rr=3;rr<=tt;rr++)
g.drawString(shi[rr], 150, rr*20-20);
if(tt>=7)
{
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
}
}
if(tt>=7&&tt<13)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
g.setColor(new Color(r,b,y));
if(tt<13) for(int rr=8;rr<=tt;rr++)
g.drawString(shi[rr], 120, rr*20-120);
//减120,是因为此时rr不是5,而是在7~13之间,减去前面的5个*20,再减20,即减120
if(tt>=13) for(int rr=8;rr<=13;rr++)
g.drawString(shi[rr], 120, rr*20-120);
}
if(tt>=13&&tt<18)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
for(int rr=8;rr<13;rr++)
g.drawString(shi[rr], 120, rr*20-120);
g.setColor(new Color(r,b,y));
if(tt<18) for(int rr=13;rr<=tt;rr++)
g.drawString(shi[rr], 90, rr*20-220);
if(tt>=18) for(int rr=13;rr<=18;rr++)
g.drawString(shi[rr], 90, rr*20-220);
}
if(tt>=18&&tt<23)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
for(int rr=8;rr<13;rr++)
g.drawString(shi[rr], 120, rr*20-120);
for(int rr=13;rr<18;rr++)
g.drawString(shi[rr], 90, rr*20-220);
g.setColor(new Color(r,b,y));
if(tt<23) for(int rr=18;rr<=tt;rr++)
g.drawString(shi[rr], 60, rr*20-320);
if(tt>=23) for(int rr=18;rr<=23;rr++)
g.drawString(shi[rr], 60, rr*20-320);
}
if(tt>=23&&tt<30)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
for(int rr=8;rr<13;rr++)
g.drawString(shi[rr], 120, rr*20-120);
for(int rr=13;rr<18;rr++)
g.drawString(shi[rr], 90, rr*20-220);
for(int rr=18;rr<23;rr++)
g.drawString(shi[rr], 60, rr*20-320);
g.setColor(new Color(r,b,y));
if(tt<30) for(int rr=23;rr<=tt;rr++)
g.drawString(shi[rr], 30, rr*20-400);
if(tt>=30) for(int rr=23;rr<=30;rr++)
g.drawString(shi[rr], 30, rr*20-400);
}
if(tt>=30)
{
g.setColor(new Color(230,0,0));
for(int rr=0;rr<3;rr++)
g.drawString(shi[rr], 170, 60+rr*20);
for(int rr=3;rr<=7;rr++)
g.drawString(shi[rr], 150, rr*20-20);
for(int rr=8;rr<13;rr++)
g.drawString(shi[rr], 120, rr*20-120);
for(int rr=13;rr<18;rr++)
g.drawString(shi[rr], 90, rr*20-220);
for(int rr=18;rr<23;rr++)
g.drawString(shi[rr], 60, rr*20-320);
for(int rr=23;rr<30;rr++)
g.drawString(shi[rr], 30, rr*20-400);
}
}
}
界面_com.mhl.view/UserLogin.java
/*
* 登录界面
*/
package com.mhl.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import com.mhl.model.*;
import com.mhl.tools.*;
import javax.imageio.*;
public class UserLogin extends JDialog implements ActionListener{
//定义需要的组件
JLabel jl1,jl2,jl3;
JTextField jName;
JPasswordField jPasswd;
JButton jCon,jCancel;
public static void main(String []args)
{
UserLogin ul=new UserLogin();
}
public UserLogin()
{
this.setLayout(null);
//创建一个BackImage对象
BackImage bi=new BackImage();
//把位置确定
bi.setBounds(0,0,660,660);
//定义一个容器
Container ct=this.getContentPane();
//创建各个组件
//用户名jl1标签
jl1=new JLabel("请输入用户名:");
jl1.setFont(MyTools.f1);
jl1.setBounds(60,190,150,30);
//放入jl1
//this.add(jl1);
ct.add(jl1); //这样也可以
//用户名jName文本框
jName=new JTextField(20);
jName.setFocusable(true);//设置成焦点
jName.setBounds(180,190,120,30);
jName.setFont(MyTools.f1);
//向下凹陷
jName.setBorder(BorderFactory.createLoweredBevelBorder());
ct.add(jName);
//员工号标签
jl2=new JLabel("(员工号)");
jl2.setFont(MyTools.f2);
jl2.setForeground(Color.RED);//设置前景色
jl2.setBounds(100,210,100,30);
ct.add(jl2);
//密码标签
jl3=new JLabel("请输入密码:");
jl3.setFont(MyTools.f1);
jl3.setBounds(60,240,150,30);
ct.add(jl3);
//密码框
jPasswd=new JPasswordField(20);
jPasswd.setBounds(180,240,120,30);
jPasswd.setFont(MyTools.f1);
jPasswd.setBorder(BorderFactory.createLoweredBevelBorder());
ct.add(jPasswd);
//确定按钮
jCon=new JButton("确定");
jCon.addActionListener(this);//监听
jCon.setFont(MyTools.f1);
jCon.setBounds(110,300,70,30);
//放入到容器
ct.add(jCon);
//取消按钮
jCancel=new JButton("取消");
jCancel.addActionListener(this);//监听
jCancel.setFont(MyTools.f1);
jCancel.setBounds(200,300,70,30);
//放入到容器
ct.add(jCancel);
//把一组件放入到JFrame,或者JDialog中可以直接放入
this.add(bi);
//也可以定义一个容器,再利用容器对象加入
//ct.add(bi);//加入背景图片
this.setUndecorated(true);//不使用上下框
this.setSize(360,360);
//确定登录框的初始位置
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width/2-200,height/2-150);
this.setVisible(true);
}
//内部类
class BackImage extends JPanel
{
Image im;
public BackImage()//加载背景图片
{
try {
im=ImageIO.read(new File("image/login.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g)//画上背景图片
{
g.drawImage(im, 0, 0, 360, 360, this);
}
}
//响应用户登录的请求
@Override
public void actionPerformed(ActionEvent e) {
//判断是否点击确定按钮
if(e.getSource()==jCon)
{
//System.out.println("确定");
//取出员工号,密码
String uid=this.jName.getText().trim();
String p=new String(this.jPasswd.getPassword());
//System.out.println(uid+" "+p);
UserModel um=new UserModel();
String res=um.checkUser(uid, p);
if(""==res)
{
JOptionPane.showMessageDialog(this,"您输入的用户名或密码有误,请重新输入!");
return;
}
//System.out.println(uid+"职位是:"+res);
if(res.equals("经理")||res.equals("主管")||res.equals("管理员"))
{
new Windows1();
//关闭登录界面
this.dispose();
}
else
{
JOptionPane.showMessageDialog(this,"你无权登录!");
}
}
else if(e.getSource()==this.jCancel)
{
//关闭登录界面
this.dispose();
}
}
}
界面(主界面)_com.mhl.view/Windows.java
/*
* 系统管理界面
* 这是系统管理员,经理,主管可以进入的操作界面
* 完成界面的顺序,从上到下,从左到右
* 写代码要注意就近原则
*/
package com.mhl.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.util.*;
import java.util.Timer;
import com.mhl.tools.*;//引入这个自己添加的包
import java.io.File;
import java.io.IOException;
public class Windows1 extends JFrame implements ActionListener,MouseListener{
//定义需要的组件
//ImageIcon titleIcon;
Image titleIcon,timebg;
JMenuBar jmb;//菜单栏
//一级菜单
JMenu jm1,jm2,jm3,jm4,jm5,jm6;
//二级菜单
JMenuItem jmm1,jmm2,jmm3,jmm4,jmm5;
//菜单项图标
ImageIcon jmm1_icon1,jmm2_icon2,jmm3_icon3,jmm4_icon4,jmm5_icon5;
//工具栏
JToolBar jtb;
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb10;
//定义需要的五个JPanel
JPanel p1,p2,p3,p4,p5;
JLabel timeNow; //用于显示当前时间的标签
//8个图标
JLabel p1_lab1,p1_lab2,p1_lab3,p1_lab4,p1_lab5,p1_lab6,p1_lab7,p1_lab8;
//给p2面板定义需要的JLabel
JLabel p2_lab1,p2_lab2;
javax.swing.Timer t; //这个Timer类可以定时触发Action
Cursor myCursor=null;
JSplitPane jsp1; //拆分窗口
CardLayout cardP3;
CardLayout cardP2;
ImagePanel p1_imgPanel;
Image p1_bg=null;
Image zhu_image=null;
public static void main(String[] args) {
//
Windows1 w1=new Windows1();
}
//初始化菜单
public void initMenu()
{
//创建图标
jmm1_icon1=new ImageIcon("image/menuBar_image/jm1_icon1.jpg");
jmm2_icon2=new ImageIcon("image/menuBar_image/jm1_icon2.jpg");
jmm3_icon3=new ImageIcon("image/menuBar_image/jm1_icon3.jpg");
jmm4_icon4=new ImageIcon("image/menuBar_image/jm1_icon4.jpg");
jmm5_icon5=new ImageIcon("image/menuBar_image/jm1_icon5.jpg");
//创建一级菜单
jm1=new JMenu("系统管理");
jm1.setFont(MyTools.f1);
//创建它的二级菜单
jmm1=new JMenuItem("切换用户",jmm1_icon1);
jmm1.setFont(MyTools.f2);
jmm2=new JMenuItem("切换到收款界面",jmm2_icon2);
jmm2.setFont(MyTools.f2);
jmm3=new JMenuItem("登陆管理",jmm3_icon3);
jmm3.setFont(MyTools.f2);
jmm4=new JMenuItem("万年历",jmm4_icon4);
jmm4.setFont(MyTools.f2);
jmm5=new JMenuItem("退出",jmm5_icon5);
jmm5.setFont(MyTools.f2);
//加入
jm1.add(jmm1);
jm1.add(jmm2);
jm1.add(jmm3);
jm1.add(jmm4);
jm1.add(jmm5);
jm2=new JMenu("人事管理");
jm2.setFont(MyTools.f1);
jm3=new JMenu("菜单管理");
jm3.setFont(MyTools.f1);
jm4=new JMenu("报表统计");
jm4.setFont(MyTools.f1);
jm5=new JMenu("成本及库房");
jm5.setFont(MyTools.f1);
jm6=new JMenu("帮助");
jm6.setFont(MyTools.f1);
//把一级菜单加入大JMenuBar
jmb=new JMenuBar();
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
jmb.add(jm5);
jmb.add(jm6);
//把JMenuBar添加到JFrame
this.setJMenuBar(jmb);
}
//初始化工具栏
public void initToolBar()
{
//处理工具栏的组件
jtb=new JToolBar();
//设置工具栏不可以浮动
jtb.setFloatable(false);
jb1=new JButton(new ImageIcon("image/toolBar_image/jb1.jpg"));
jb2=new JButton(new ImageIcon("image/toolBar_image/jb2.jpg"));
jb3=new JButton(new ImageIcon("image/toolBar_image/jb3.jpg"));
jb4=new JButton(new ImageIcon("image/toolBar_image/jb4.jpg"));
jb5=new JButton(new ImageIcon("image/toolBar_image/jb5.jpg"));
jb6=new JButton(new ImageIcon("image/toolBar_image/jb6.jpg"));
jb7=new JButton(new ImageIcon("image/toolBar_image/jb7.jpg"));
jb8=new JButton(new ImageIcon("image/toolBar_image/jb8.jpg"));
jb9=new JButton(new ImageIcon("image/toolBar_image/jb9.jpg"));
jb10=new JButton(new ImageIcon("image/toolBar_image/jb10.jpg"));
//把按钮加入到jtb
jtb.add(jb1);
jtb.add(jb2);
jtb.add(jb3);
jtb.add(jb4);
jtb.add(jb5);
jtb.add(jb6);
jtb.add(jb7);
jtb.add(jb8);
jtb.add(jb9);
jtb.add(jb10);
}
//初始化中间的panel
public void allPanels()
{
//处理p1面板
p1=new JPanel(new BorderLayout());
try {
p1_bg = ImageIO.read(new File("image/center_image/jp1_bg.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
myCursor=new Cursor(Cursor.HAND_CURSOR);//设置手形光标
//p1的背景图
this.p1_imgPanel=new ImagePanel(p1_bg);
this.p1_imgPanel.setLayout(new GridLayout(8,1));
p1_lab1=new JLabel(new ImageIcon("image/center_image/label_1.gif"));
p1_lab1.setFont(MyTools.f3);
p1_lab1.setCursor(myCursor);
p1_lab1.addMouseListener(this);
this.p1_imgPanel.add(p1_lab1); //加入第1个label
p1_lab2=new JLabel("人 事 管 理",new ImageIcon("image/center_image/label_2.jpg"),0);
p1_lab2.setFont(MyTools.f3);
p1_lab2.setEnabled(false);//让该label不可用
p1_lab2.addMouseListener(this);
p1_lab2.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab2);
p1_lab3=new JLabel("登 录 管 理",new ImageIcon("image/center_image/label_3.jpg"),0);
p1_lab3.setFont(MyTools.f3);
p1_lab3.setEnabled(false);
p1_lab3.addMouseListener(this);
p1_lab3.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab3);
p1_lab4=new JLabel("菜 谱 价 格",new ImageIcon("image/center_image/label_4.jpg"),0);
p1_lab4.setFont(MyTools.f3);
p1_lab4.setEnabled(false);
p1_lab4.addMouseListener(this);
p1_lab4.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab4);
p1_lab5=new JLabel("报 表 统 计",new ImageIcon("image/center_image/label_5.jpg"),0);
p1_lab5.setFont(MyTools.f3);
p1_lab5.setEnabled(false);
p1_lab5.addMouseListener(this);
p1_lab5.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab5);
p1_lab6=new JLabel("成本及库房",new ImageIcon("image/center_image/label_6.jpg"),0);
p1_lab6.setFont(MyTools.f3);
p1_lab6.setEnabled(false);
p1_lab6.addMouseListener(this);
p1_lab6.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab6);
p1_lab7=new JLabel("系 统 设 置",new ImageIcon("image/center_image/label_7.jpg"),0);
p1_lab7.setFont(MyTools.f3);
p1_lab7.setEnabled(false);
p1_lab7.addMouseListener(this);
p1_lab7.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab7);
p1_lab8=new JLabel("动 画 帮 助",new ImageIcon("image/center_image/label_8.jpg"),0);
p1_lab8.setFont(MyTools.f3);
p1_lab8.setEnabled(false);
p1_lab8.addMouseListener(this);
p1_lab8.setCursor(myCursor);
this.p1_imgPanel.add(p1_lab8);
p1.add(this.p1_imgPanel);
//处理p2,p3,p4
p4=new JPanel(new BorderLayout());
cardP2=new CardLayout();
p2=new JPanel(this.cardP2);//卡片布局
p2_lab1=new JLabel(new ImageIcon("image/center_image/jp2_left.jpg"));
p2_lab1.addMouseListener(this); //监听
p2_lab2=new JLabel(new ImageIcon("image/center_image/jp2_right.jpg"));
p2_lab2.addMouseListener(this); //监听
p2.add(p2_lab1,"0");//作为p2的0号卡片
p2.add(p2_lab2,"1");//作为p2的1号卡片
this.cardP3=new CardLayout();
p3=new JPanel(this.cardP3);
//先给p3加入一个主界面的卡片(ImagePanel)
try {
zhu_image = ImageIO.read(new File("image/center_image/jp3_bg.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
ImagePanel ip=new ImagePanel(zhu_image);
p3.add(ip,"0");//作为p3的0号卡片
//对p3做添加几个JLabel
//创建EmpInfo对象(JPanel)
Empinfo eInfo=new Empinfo();
p3.add(eInfo,"1"); //人事管理
JLabel dl=new JLabel(new ImageIcon("image/center_image/denglu_img.jpg"));
p3.add(dl,"2"); //登录管理
//把p2,p3加入p4
p4.add(p2,"West");
p4.add(p3,"Center");
//做一个拆分窗口,分别存放p1和p4
jsp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p1,p4);//横向分拆
//指定左边的面板占多大
jsp1.setDividerLocation(160);
//设置分隔线的宽度
jsp1.setDividerSize(0);
}
//初始化状态栏
public void initStatusBar()
{
//处理p5面板
p5=new JPanel(new BorderLayout());
//创建Timer
t=new javax.swing.Timer(1000,this); //每隔1秒取触发ActionListener
t.start();//启动定时器
//获取当前时间
timeNow=new JLabel("当前时间:"+Calendar.getInstance().getTime().toString());
//this.timeNow.setText(Calendar.getInstance().getTime().toLocaleString());
//timeNow.setFont(MyTools.f1); //设置字体
//设置状态栏背景
try {
timebg=ImageIO.read(new File("image/time_bg.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
ImagePanel ip1=new ImagePanel(timebg);//建一个存放图片的panel
ip1.setLayout(new BorderLayout());
ip1.add(timeNow,"East");
p5.add(ip1);
}
public Windows1()
{
//创建组件
//titleIcon=new ImageIcon("image/title.ico");
try {
titleIcon=ImageIO.read(new File("image/title.gif"));
} catch (Exception e) {
//
e.printStackTrace();
}
//调用初始化菜单函数
this.initMenu();
//调用初始化工具栏函数
this.initToolBar();
//调用初始化中间面板的函数
this.allPanels();
//调用初始化状态栏的函数
this.initStatusBar();
//从JFrame中取得Container
Container ct=this.getContentPane();
ct.add(jtb,"North");
ct.add(jsp1,"Center");
ct.add(p5,"South");
//设置大小
int w=Toolkit.getDefaultToolkit().getScreenSize().width ;
int h=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setIconImage(titleIcon);//设置窗口图标
this.setTitle("满汉楼餐饮管理系统");
//关闭窗口时候,退出系统
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(w,h-30);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//
this.timeNow.setText("当前时间:"+Calendar.getInstance().getTime().toString());
//this.timeNow.setText(Calendar.getInstance().getTime().toLocaleString());
}
@Override
public void mouseClicked(MouseEvent e) {
//判断用户点击哪个操作JLabel
if(e.getSource()==this.p1_lab1)
{
this.cardP3.show(this.p3, "0");
}
else if(e.getSource()==this.p1_lab2)
{
this.cardP3.show(this.p3, "1");
}
else if(e.getSource()==this.p1_lab3)
{
this.cardP3.show(this.p3, "2");
}
else if(e.getSource()==this.p2_lab1)//当点击了向左的箭头
{
//把显示各种操作的界面p1,展开
this.cardP2.show(p2, "1"); //显示向右的箭头
this.jsp1.setDividerLocation(0);//隐藏
}
else if(e.getSource()==this.p2_lab2) //当点击了向右的箭头
{
this.cardP2.show(p2, "0"); //显示向左的箭头
this.jsp1.setDividerLocation(160);//展开
}
}
@Override
public void mouseEntered(MouseEvent e) {
//如果用户选择了某个操作JLabel,则高亮
if(e.getSource()==this.p1_lab2)
{
//System.out.println("选中了 人事管理");
this.p1_lab2.setEnabled(true);
}
else if(e.getSource()==this.p1_lab3)
{
this.p1_lab3.setEnabled(true);
}
else if(e.getSource()==this.p1_lab4)
{
this.p1_lab4.setEnabled(true);
}
else if(e.getSource()==this.p1_lab5)
{
this.p1_lab5.setEnabled(true);
}
else if(e.getSource()==this.p1_lab6)
{
this.p1_lab6.setEnabled(true);
}
else if(e.getSource()==this.p1_lab7)
{
this.p1_lab7.setEnabled(true);
}
else if(e.getSource()==this.p1_lab8)
{
this.p1_lab8.setEnabled(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
//如果用户选择了某个操作JLabel,则禁用
if(e.getSource()==this.p1_lab2)
{
this.p1_lab2.setEnabled(false);
}
else if(e.getSource()==this.p1_lab3)
{
this.p1_lab3.setEnabled(false);
}
else if(e.getSource()==this.p1_lab4)
{
this.p1_lab4.setEnabled(false);
}
else if(e.getSource()==this.p1_lab5)
{
this.p1_lab5.setEnabled(false);
}
else if(e.getSource()==this.p1_lab6)
{
this.p1_lab6.setEnabled(false);
}
else if(e.getSource()==this.p1_lab7)
{
this.p1_lab7.setEnabled(false);
}
else if(e.getSource()==this.p1_lab8)
{
this.p1_lab8.setEnabled(false);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
界面_com.mhl.view/Empinfo.java
/*
* 用于显示人事管理的界面
*/
package com.mhl.view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.mhl.model.EmpModel;
import com.mhl.tools.MyTools;
public class Empinfo extends JPanel implements ActionListener{
//定义需要的各个组件
JPanel p1,p2,p3,p4,p5;
JLabel p1_lab1,p3_lab1;
JTextField p1_jtf1;
JButton p1_jb1,p4_jb1,p4_jb2,p4_jb3,p4_jb4;
//这个是用于显示人事信息的tabel
JTable jtable; //表格面板
JScrollPane jsp;//滚动面板
EmpModel em=null; //定义数据模型
//构造函数
public Empinfo()
{
//创建需要的组件
//处理p1(北面的)
p1=new JPanel(new FlowLayout(FlowLayout.CENTER));
p1_lab1=new JLabel("请输入姓名(员工号或职位):");
p1_lab1.setFont(MyTools.f2);
p1_jtf1=new JTextField(20);
p1_jb1=new JButton("查询");
p1_jb1.setFont(MyTools.f3);
//把他们加入到p1
p1.add(p1_lab1);
p1.add(p1_jtf1);
p1.add(p1_jb1);
//处理p2(中间的)
em=new EmpModel();
String []paras={"1"};
em.query("select cleId,cleName,cleSex,cleZw from clerkInfo where 1=?",paras);
jtable=new JTable(em);
p2=new JPanel(new BorderLayout());
jsp=new JScrollPane(jtable);
p2.add(jsp);
//p2.add(jtable);
//处理p3(南面的)
p3=new JPanel(new FlowLayout(FlowLayout.LEFT));
p3_lab1=new JLabel("总记录数是??条");
p3_lab1.setFont(MyTools.f2);
p3.add(p3_lab1);
p4=new JPanel(new FlowLayout(FlowLayout.RIGHT));
p4_jb1=new JButton("详细信息");
p4_jb1.setFont(MyTools.f3);
p4_jb2=new JButton("添加");
p4_jb2.setFont(MyTools.f3);
p4_jb3=new JButton("修改");
p4_jb3.setFont(MyTools.f3);
p4_jb4=new JButton("删除");
p4_jb4.addActionListener(this);
p4_jb4.setFont(MyTools.f3);
p4.add(p4_jb1);
p4.add(p4_jb2);
p4.add(p4_jb3);
p4.add(p4_jb4);
p5=new JPanel(new BorderLayout());
p5.add(p3,"West");
p5.add(p4,"East");
//把总JPanel设为BorderLayout
this.setLayout(new BorderLayout());
//把p1加入到总的JPanel
this.add(p1,"North");//北面
this.add(p2,"Center");//中间
this.add(p5,"South");//南面
//this.setBackground(Color.pink);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//如果用户要删除某个员工
if(this.p4_jb4==e.getSource())
{
//System.out.println("删除");
int selRowNum=jtable.getSelectedRow();
String empNo=(String)em.getValueAt(selRowNum, 0);
System.out.println("员工编号:"+empNo);
if(em.deEmpById(empNo))
{
JOptionPane.showMessageDialog(null, "删除成功");
}
else
if(em.deEmpById(empNo))
{
JOptionPane.showMessageDialog(null, "删除失败");
}
EmpModel em=new EmpModel();
String []paras={"1"};
em.query("select cleId,cleName,cleSex,cleZw from clerkInfo where 1=?", paras);
jtable.setModel(em); //更新数据模型
}
}
}
界面_com.mhl.model/UserModel.java
/*
* 这是用户表数据模型,用它完成对用户的各种操作,
* 这里主要编写项目需要的业务操作
*/
package com.mhl.model;
import com.mhl.db.*;
import java.sql.*;
public class UserModel {
/**
*
* @param uid 用编号
* @param p 用户密码
* @return 该用户的职位,如果该用户不存在则返回空。
*/
//验证用户
public String checkUser(String uid,String p)
{
String zhiwei="";
SqlHelper sqlh=null;
try {
//组织sql,和参数列表
String sql="select clerkInfo.cleZw from login,clerkInfo where login.cleId=clerkInfo.cleId " +
"and login.cleId=? and login.passwd=?";
String paras[]={uid,p};
sqlh=new SqlHelper();
ResultSet rs=sqlh.query(sql,paras);
if(rs.next())
{
//则取出职位
zhiwei=rs.getString(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
sqlh.close();//关闭资源
}
return zhiwei;
}
}
界面_com.mhl.model/EmpModel.java
/**
* 人事的数据模型类,完成对人事表的各种操作
*/
package com.mhl.model;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
import com.mhl.db.SqlHelper;
import com.mhl.model.*;
public class EmpModel extends AbstractTableModel{
Vector<String> colums;
Vector<Vector> rows;
//完成增,删,改的方法
//删,依据员工号
public boolean deEmpById(String empId)
{
boolean b=true;
String sql="delete from clerkInfo where cleId=?";
String []paras={empId};
SqlHelper sh=new SqlHelper();
try {
b=sh.exeUpdate(sql, paras);
} catch (Exception e) {
e.printStackTrace();
}finally{
sh.close();
}
return b;
}
//写一个方法,用于查询需要显示的人事信息
//query方法不通用。。。,对query做一个修改,让它具有更好的通用性
public void query(String sql,String paras[])
{
//初始化列
this.colums=new Vector<String>();
this.rows=new Vector<Vector>();
//创建SqlHelper对象
SqlHelper sh=new SqlHelper();
ResultSet rs=sh.query(sql, paras);
try {
//rs对象中可以得到一个ResultSetMetaData
ResultSetMetaData rsmd=rs.getMetaData();
//rsmd可以得到结果有多少列,而且可以知道每列的名字
for(int i=0;i<rsmd.getColumnCount();i++)
{
this.colums.add(rsmd.getColumnName(i+1));
//得到第i+1列的列名,并存到colums中
}
//把rs的结果放入到rows
while(rs.next())
{
Vector<String> temp=new Vector<String>();
for(int i=0;i<rsmd.getColumnCount();i++)
{
temp.add(rs.getString(i+1));
}
rows.add(temp);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
sh.close();
}
}
@Override //返回列数
public int getColumnCount() {
// TODO Auto-generated method stub
return this.colums.size();
}
@Override //返回行数
public int getRowCount() {
// TODO Auto-generated method stub
return this.rows.size();
}
@Override //返回字段的值
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)rows.get(rowIndex)).get(columnIndex);
}
/* (non-Javadoc)
* @see javax.swing.table.AbstractTableModel#getColumnName(int)
*/
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return this.colums.get(column).toString();
}
}
界面_com.mhl.db/SqlHelper.java
/**
* 对数据库进行操作的类
* 对数据库的操作,就是crud
* 调用存储过程
*/
package com.mhl.db;
import java.util.*;
import java.sql.*;
public class SqlHelper {
//定义需要的对象
PreparedStatement ps=null;
ResultSet rs=null;
Connection ct=null;
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=MyRestaurantDB";
String user="sa";
String passwd="luowei";
//构造函数,初始化ct
public SqlHelper()
{
try {
Class.forName(driverName);
ct=DriverManager.getConnection(url,user,passwd);
} catch (Exception e) {
e.printStackTrace();
}
}
//把对数据库的增,删,改写一个函数
public boolean exeUpdate(String sql,String [] paras)
{
boolean b=true;
try {
ps=ct.prepareStatement(sql);
//对sql的参数赋值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i]);
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
public ResultSet query(String sql,String []paras) //查询
{
try {
ps=ct.prepareStatement(sql);
//对sql的参数赋值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i]);
}
rs=ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
//关闭资源
public void close()
{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(!ct.isClosed()) ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}