为了防止使用的人找不到文件的具体位置,我将我的文件布局截屏如下,只需要按照下图创建,就可以正确运行啦,
下面我就一一贴上具体的代码
Caozuo类
//自己写的
package cy.jiemian;
import zidingyi.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.Timer;
import java.util.*;
import javax.imageio.*;
public class Caozuo extends JFrame implements ActionListener,MouseListener{
Image titleIcon,timebg;
JMenuBar jmb;
JMenu jm1,jm2,jm3,jm4,jm5,jm6;
JMenuItem jmm1,jmm2,jmm3,jmm4,jmm5;
ImageIcon jm1_icon1,jm1_icon2,jm1_icon3,jm1_icon4,jm1_icon5;
JToolBar jtb;
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb10;
JPanel jp1,jp2,jp3,jp4,jp5;
JPanel jp3_1,jp3_2,jp3_3,jp3_4,jp3_5,jp3_6,jp3_7;
ImagePanel jp1_imgPanel;
Image jp1_bg;
JLabel jp1_lab1,jp1_lab2,jp1_lab3,jp1_lab4,jp1_lab5,jp1_lab6,jp1_lab7,jp1_lab8;
JLabel jp2_lab1,jp2_lab2;
Image jp3_bg;
JLabel jp3_l1,jp3_l2,jp3_l3,jp3_l4,jp3_l5,jp3_l6,jp3_l7;
CardLayout cardjp2,cardjp3;
JSplitPane jsp1;//左右拆分功能
JLabel timeNow;
javax.swing.Timer t;//时间
public static void main(String[] args) {
Caozuo cz=new Caozuo();
}
public void initMenu() {
jm1_icon1=new ImageIcon("image/jm1_icon1.jpg");
jm1_icon2=new ImageIcon("image/jm1_icon2.jpg");
jm1_icon3=new ImageIcon("image/jm1_icon3.jpg");
jm1_icon4=new ImageIcon("image/jm1_icon4.jpg");
jm1_icon5=new ImageIcon("image/jm1_icon5.jpg");
jm1=new JMenu("系统管理");
jm1.setFont(MyFont.f1);
jmm1=new JMenuItem("切换用户",jm1_icon1);
jmm1.setFont(MyFont.f2);
jmm2=new JMenuItem("切换到收款界面",jm1_icon2);
jmm2.setFont(MyFont.f2);
jmm3=new JMenuItem("登陆管理",jm1_icon3);
jmm3.setFont(MyFont.f2);
jmm4=new JMenuItem("万年历",jm1_icon4);
jmm4.setFont(MyFont.f2);
jmm5=new JMenuItem("退出",jm1_icon5);
jmm5.setFont(MyFont.f2);
jm1.add(jmm1);
jm1.add(jmm2);
jm1.add(jmm3);
jm1.add(jmm4);
jm1.add(jmm5);
jm2=new JMenu("人事管理");
jm2.setFont(MyFont.f1);
jm3=new JMenu("菜单服务");
jm3.setFont(MyFont.f1);
jm4=new JMenu("报表统计");
jm4.setFont(MyFont.f1);
jm5=new JMenu("成本及库房");
jm5.setFont(MyFont.f1);
jm6=new JMenu("帮助");
jm6.setFont(MyFont.f1);
jmb=new JMenuBar();
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
jmb.add(jm5);
jmb.add(jm6);
this.setJMenuBar(jmb);
}
public void initToolBar() {
jtb=new JToolBar();
jtb.setFloatable(false);
jb1=new JButton(new ImageIcon("image/jb1.jpg"));
jb2=new JButton(new ImageIcon("image/jb2.jpg"));
jb3=new JButton(new ImageIcon("image/jb3.jpg"));
jb4=new JButton(new ImageIcon("image/jb4.jpg"));
jb5=new JButton(new ImageIcon("image/jb5.jpg"));
jb6=new JButton(new ImageIcon("image/jb6.jpg"));
jb7=new JButton(new ImageIcon("image/jb7.jpg"));
jb8=new JButton(new ImageIcon("image/jb8.jpg"));
jb9=new JButton(new ImageIcon("image/jb9.jpg"));
jb10=new JButton(new ImageIcon("image/jb10.jpg"));
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);
this.add(jtb,BorderLayout.NORTH);
}
public void initAllPanel() {
jp1=new JPanel(new BorderLayout());
try {
jp1_bg=ImageIO.read(new File("image/jp1_bg.jpg"));
}catch(Exception e) {}
Cursor myCursor=new Cursor(Cursor.HAND_CURSOR);
//下两句是xidingyi函数写完加的
jp1_imgPanel = new ImagePanel(jp1_bg);
this.jp1_imgPanel.setLayout(new GridLayout(8,1));
jp1_lab1=new JLabel(new ImageIcon("image/label_1.gif"));
jp1_imgPanel.add(jp1_lab1);
jp1_lab2=new JLabel("人事管理",new ImageIcon("image/label_2.jpg"), 0);
jp1_lab2.setCursor(myCursor);
jp1_lab2.setEnabled(false);
jp1_lab2.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab2);
jp1_lab2.addMouseListener(this);
jp1_lab3=new JLabel("登陆管理",new ImageIcon("image/label_3.jpg"), 0);
jp1_lab3.setCursor(myCursor);
jp1_lab3.setEnabled(false);
jp1_lab3.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab3);
jp1_lab3.addMouseListener(this);
jp1_lab4=new JLabel("菜谱价格",new ImageIcon("image/label_4.jpg"), 0);
jp1_lab4.setCursor(myCursor);
jp1_lab4.setEnabled(false);
jp1_lab4.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab4);
jp1_lab4.addMouseListener(this);
jp1_lab5=new JLabel("报表统计",new ImageIcon("image/label_5.jpg"), 0);
jp1_lab5.setEnabled(false);
jp1_lab5.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab5);
jp1_lab5.addMouseListener(this);
jp1_lab6=new JLabel("成本及库房",new ImageIcon("image/label_6.jpg"), 0);
jp1_lab6.setCursor(myCursor);
jp1_lab6.setEnabled(false);
jp1_lab6.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab6);
jp1_lab6.addMouseListener(this);
jp1_lab7=new JLabel("系统设置",new ImageIcon("image/label_7.jpg"), 0);
jp1_lab7.setCursor(myCursor);
jp1_lab7.setEnabled(false);
jp1_lab7.setFont(MyFont.f4);
jp1_imgPanel.add(jp1_lab7);
jp1_lab7.addMouseListener(this);
jp1_lab8=new JLabel("动画帮助",new ImageIcon("image/label_8.jpg"),0);
jp1_lab8.setCursor(myCursor);
jp1_lab8.setEnabled(false);
jp1_lab8.setFont(MyFont.f4);
jp1_lab8.addMouseListener(this);
jp1_imgPanel.add(jp1_lab8);
jp1.add(jp1_imgPanel);
jp4=new JPanel(new BorderLayout());
this.cardjp2=new CardLayout();
jp2=new JPanel(cardjp2);
jp2_lab1=new JLabel(new ImageIcon("image/jp2_left.jpg"));
jp2_lab1.addMouseListener(this);
jp2_lab2=new JLabel(new ImageIcon("image/jp2_right.jpg"));
jp2_lab2.addMouseListener(this);
jp2.add(jp2_lab1,"0");
jp2.add(jp2_lab2,"1");
this.cardjp3=new CardLayout();
jp3=new JPanel(cardjp3);
try {
jp3_bg=ImageIO.read(new File("image/jp3_5.jpg"));
}catch(Exception e) {}
jp3_l1=new JLabel(new ImageIcon("image/jp3_1.jpg"));
jp3_l2=new JLabel(new ImageIcon("image/jp3_2.jpg"));
jp3_l3=new JLabel(new ImageIcon("image/jp3_3.jpg"));
jp3_l4=new JLabel(new ImageIcon("image/jp3_4.jpg"));
jp3_l5=new JLabel(new ImageIcon("image/jp3_5.jpg"));
jp3_l6=new JLabel(new ImageIcon("image/jp3_6.jpg"));
jp3_l7=new JLabel(new ImageIcon("image/jp3_7.jpg"));
jp3_1=new JPanel();
jp3_1.add(jp3_l1);
jp3_2=new JPanel();
jp3_2.add(jp3_l2);
jp3_3=new JPanel();
jp3_3.add(jp3_l3);
jp3_4=new JPanel();
jp3_4.add(jp3_l4);
jp3_5=new JPanel();
jp3_5.add(jp3_l5);
jp3_6=new JPanel();
jp3_6.add(jp3_l6);
jp3_7=new JPanel();
jp3_7.add(jp3_l7);
jp3.add(jp3_1,"1");
jp3.add(jp3_2,"2");
jp3.add(jp3_3,"3");
jp3.add(jp3_4,"4");
jp3.add(jp3_5,"5");
jp3.add(jp3_6,"6");
jp3.add(jp3_7,"7");
jp4.add(jp2,BorderLayout.WEST);
jp4.add(jp3,BorderLayout.CENTER);
jsp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,jp1,jp4);//添加了jp1和jp4才可以分割
jsp1.setDividerLocation(150);
jsp1.setDividerSize(0);
this.add(jsp1,BorderLayout.CENTER);
}
public void initJp5() {//分区的更合理
jp5=new JPanel();
jp5.setLayout(new BorderLayout());
t=new Timer(1000,this);//可以让时间随时更新,一秒一变
t.start();
timeNow=new JLabel(Calendar.getInstance().getTime().toLocaleString());//加了个Icon
try {
timebg=ImageIO.read(new File("image/time_bg.jpg"));
}catch(Exception e) {}
ImagePanel ip1=new ImagePanel(timebg);
ip1.setLayout(new BorderLayout());
ip1.add(timeNow,BorderLayout.EAST);
jp5.add(ip1);
this.add(jp5,BorderLayout.SOUTH);
}
public Caozuo() {
try {
titleIcon=ImageIO.read(new File("image/title.gif"));
}catch(IOException e) {}
this.setIconImage(titleIcon);
this.setTitle("满汉楼餐饮管理系统");
this.setFont(MyFont.f0);
this.initMenu();
this.initToolBar();
this.initAllPanel();
this.initJp5();
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(width, height-32);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.timeNow.setText("当前系统时间:"+Calendar.getInstance().getTime().toLocaleString());
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==this.jp1_lab2) {
this.cardjp3.show(jp3, "1");
}
else if(e.getSource()==this.jp1_lab3) {
this.cardjp3.show(jp3, "2");//切换图片
}else if(e.getSource()==this.jp1_lab4) {
this.cardjp3.show(jp3, "3");
}else if(e.getSource()==this.jp1_lab5) {
this.cardjp3.show(jp3, "4");
}else if(e.getSource()==this.jp1_lab6) {
this.cardjp3.show(jp3, "5");
}else if(e.getSource()==this.jp1_lab7) {
this.cardjp3.show(jp3, "6");
}else if(e.getSource()==this.jp1_lab8) {
this.cardjp3.show(jp3, "7");
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
//鼠标指向的功能
if(e.getSource()==this.jp1_lab2) {
this.jp1_lab2.setEnabled(true);
}
else if(e.getSource()==this.jp1_lab3) {
this.jp1_lab3.setEnabled(true);
}
else if(e.getSource()==this.jp1_lab4) {
this.jp1_lab4.setEnabled(true);//设置亮色
}
else if(e.getSource()==this.jp1_lab5) {
this.jp1_lab5.setEnabled(true);
}
else if(e.getSource()==this.jp1_lab6) {
this.jp1_lab6.setEnabled(true);
}
else if(e.getSource()==this.jp1_lab7) {
this.jp1_lab7.setEnabled(true);
}
else if(e.getSource()==this.jp1_lab8) {
this.jp1_lab8.setEnabled(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==this.jp1_lab2) {
this.jp1_lab2.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab3) {
this.jp1_lab3.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab4) {
this.jp1_lab4.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab5) {
this.jp1_lab5.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab6) {
this.jp1_lab6.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab7) {
this.jp1_lab7.setEnabled(false);
}
else if(e.getSource()==this.jp1_lab8) {
this.jp1_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
}
}
先普及一下Image类的使用方法,我一直都有点懵
1.根据一个文件路径创建Image对象
Image image = ImageIO.read(new File(filepath));
2.将Image对象保存为文件形式
BufferedImage image = ImageIO.read(new File(filepath));//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "jpg", new File("f:/image/apple.jpg"));
3.使用窗口加载,将图片变为背景显示
ImageIcon bg =new ImageIcon(image);
CardLayout的知识
CardLayout就是类似的这样一个布局管理器,它能够让多个组件共享同一个显示空间,共享空间的组件之间的关系就像重叠在一起的一幅扑克牌,组件重叠在一起,初始时显示该空间中第一个组件,通过CardLayout类提供的方法可以切换该空间中显示的组件。
Cursor 属性,全称是“Control.Cursor 属性”,意即获取或设置当鼠标指针位于控件上时显示的光标。
Caozuo类的运行结果
Denglu类
package cy.jiemian;
import cy.moxing.*;
import zidingyi.*;
import javax.imageio.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Denglu extends JDialog implements ActionListener{
JLabel jl1,jl2,jl3;
JTextField jname;
JPasswordField jpasswd;
JButton jlog,jcancle;
Font fl;
public static void main(String[] args) {
Denglu login=new Denglu();
}
public Denglu() {
jl1=new JLabel("请输入用户名:");
jl1.setBounds(60,190,150,30);//设置位置和大小
jl1.setFont(MyFont.f1);
this.add(jl1);
jl2=new JLabel("(或员工号)");
jl2.setForeground(Color.RED);
jl2.setBounds(100,210,100,30);
jl2.setFont(MyFont.f2);
this.add(jl2);
jl3=new JLabel("请输入密码:");
jl3.setBounds(60,240,150,30);
jl3.setFont(MyFont.f2);
this.add(jl3);
jname=new JTextField(20);
jname.setFocusable(true);//设置属性,使其光标默认在用户名后的文本框中
jname.setBounds(180,190,120,30);
jname.setFont(fl);
this.add(jname);
//下一句是为了让用户名的框和密码框可以对齐
jname.setBorder(BorderFactory.createLoweredBevelBorder());
jpasswd=new JPasswordField();
jpasswd.setFocusable(true);
jpasswd.setBounds(180,240,120,30);
this.add(jpasswd);
jpasswd.setBorder(BorderFactory.createLoweredBevelBorder());
jlog=new JButton("确定");
jlog.setBounds(105,300,75,30);
jlog.addActionListener(this);
jlog.setFont(fl);
jlog.setForeground(Color.BLUE);
this.add(jlog);
jcancle=new JButton("取消");
jcancle.setBounds(215,300,75,30);
jcancle.setFont(fl);
jcancle.addActionListener(this);
jcancle.setForeground(Color.BLUE);
this.add(jcancle);
this.setLayout(null);
BackImage bi=new BackImage();
bi.setBounds(0,0,360,360);//定义组件的位置和大小
this.add(bi);
// 只需要调用Frame类中的setUndecorated()方法就可以让Frame窗口失去边框和标题栏,下面两句都是
this.setUndecorated(true);//去掉原有窗口的标题栏,
this.setSize(360, 360);
int width=getToolkit().getDefaultToolkit().getScreenSize().width;
int height=getToolkit().getDefaultToolkit().getScreenSize().height;
this.setLocation(width/2-200, height/2-200);
this.setVisible(true);
}
class BackImage extends JPanel{
Image im;
public BackImage() {
try {
im=ImageIO.read(new File("image/login.gif"));
}catch(Exception e) {}
}
public void paint(Graphics g) {//重写paint函数,重绘图
// 下面的代码在坐标(0, 0)处显示一个被缩放为360象素宽360象素高的图像:
g.drawImage(im, 0, 0, 360,360,this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jlog) {
String userName=this.jname.getText().trim();//从文本框里面获取用户名
String passwd=new String(this.jpasswd.getPassword());//从文本框里面获取密码
Moxing um=new Moxing();
String zhiwei=um.checkUser(userName, passwd);
if(zhiwei.equals("经理")||zhiwei.equals("管理员")||zhiwei.equals("主管")) {
new Caozuo();//调用新的界面出来
this.dispose();//关闭登陆界面
}else {
JOptionPane.showMessageDialog(this,"用户名或者密码错误");
}
}else if(e.getSource()==jcancle) {
this.dispose();
}
}
}
Denglu类的运行结果,
Zhujiemian类
package cy.jiemian;
import javax.swing.*;
import java.awt.*;
public class Zhujiemian extends JWindow implements Runnable{
JProgressBar jpb;
JLabel jl1;
int width,height;//确定出场位置
public static void main(String[] args) {
Zhujiemian zhujiemian=new Zhujiemian();
//滚动条就是一个线程
Thread t=new Thread(zhujiemian);
t.start();
}
public Zhujiemian() {
jl1=new JLabel(new ImageIcon("image/index.gif"));
jpb=new JProgressBar();//进度条的声明
jpb.setStringPainted(true);
jpb.setIndeterminate(false);
jpb.setBorderPainted(false);
//如果界面中没有用户可操作的按钮,按钮、文本框之类的,就只设置这三个,
jpb.setForeground(Color.BLUE);//进度条的颜色
this.add(jl1,BorderLayout.NORTH);
this.add(jpb,BorderLayout.SOUTH);
this.setSize(400,265);
width=Toolkit.getDefaultToolkit().getScreenSize().width;
height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width/2-200, height/2-150);//出场位置是固定的,不管显示器多大
this.setVisible(true);
}
@Override
public void run() {
// TODO Auto-generated method stub
int []progressValue= {0,1,5,8,14,17,26,35,38,43,49,56,65,71,75,78,86,90,92,95,99,100};
for(int i=0;i<progressValue.length;i++) {
try {
Thread.sleep(900);//适当睡眠,不然速度太快看不到就到了100
}catch(Exception e) {}
jpb.setValue(progressValue[i]);//进度条的进度和数字相匹配
}
new Denglu();//调动第二个函数,才能启动第二个界面
this.dispose();//将当前页面关闭,启动第二个页面
}
}
Moxing类
package cy.moxing;
import cy.shujuku.*;
import java.sql.*;
public class Moxing {
public String checkUser(String cleId,String passwd) {//验证用户
String zhiwei="";
shujuku sh=null;
try {//用户名和密码已经验证通过,通过职位来确定登陆到哪个界面
String sql="select cleZw from clerkInfo c,login 1 where c.cleId=1.cleId and 1.cleId=? and 1.passwd=?";
String paras[]= {cleId,passwd};//对用户名和密码进行验证,如果合适的话就传过去
sh=new shujuku();
ResultSet rs=sh.query(sql, paras);
if(rs.next()) {
zhiwei=rs.getString(1);
}
}catch(Exception e) {}
finally {
sh.close();
}
return zhiwei;
}
}
Shujuku类
package cy.shujuku;
import java.util.*;
import java.sql.*;
public class shujuku {
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=xsglxt";
String user="sa";
String passwd="123456789h";
public shujuku() {
try {
Class.forName(driver);
ct=DriverManager.getConnection(url, user, passwd);
}catch(Exception e) {}
}
public ResultSet query(String sql,String []paras) {//参数是sql语句,第二个是传进来一些参数
try {
ps=ct.prepareStatement(sql);
//PreparedStatement对象不仅包含了SQL语句,而且大多数情况下这个语句已经被预编译过,
//因而当其执行时,只需DBMS运行SQL语句,而不必先编译。当你需要执行Statement对象多次的时候,
//PreparedStatement对象将会大大降低运行时间,当然也加快了访问数据库的速度。
for(int i=0;i<paras.length;i++) {
ps.setString(i+1, paras[i]);//一条一条记录取出来
}
rs=ps.executeQuery();
}catch(Exception e1) {}
return rs;
}
public void close() {
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
}catch(Exception e2) {}
}
}
ImagePanel类
package cy.shujuku;
import java.util.*;
import java.sql.*;
public class shujuku {
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=xsglxt";
String user="sa";
String passwd="123456789h";
public shujuku() {
try {
Class.forName(driver);
ct=DriverManager.getConnection(url, user, passwd);
}catch(Exception e) {}
}
public ResultSet query(String sql,String []paras) {//参数是sql语句,第二个是传进来一些参数
try {
ps=ct.prepareStatement(sql);
//PreparedStatement对象不仅包含了SQL语句,而且大多数情况下这个语句已经被预编译过,
//因而当其执行时,只需DBMS运行SQL语句,而不必先编译。当你需要执行Statement对象多次的时候,
//PreparedStatement对象将会大大降低运行时间,当然也加快了访问数据库的速度。
for(int i=0;i<paras.length;i++) {
ps.setString(i+1, paras[i]);//一条一条记录取出来
}
rs=ps.executeQuery();
}catch(Exception e1) {}
return rs;
}
public void close() {
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
}catch(Exception e2) {}
}
}
MyFont类
package zidingyi;
import java.awt.Font;
//把所有的字体放进一个类里,用的时候调用就可以了
public class MyFont {
public static Font f0=new Font("宋体",Font.PLAIN,18);
public static Font f1=new Font("宋体",Font.PLAIN,16);
public static Font f2=new Font("宋体",Font.PLAIN,14);
public static Font f3=new Font("宋体",Font.PLAIN,15);
public static Font f4=new Font("宋体",Font.PLAIN,13);
}