Java案例指导书之水果管理系统.docx
- 文档编号:14528816
- 上传时间:2023-06-24
- 格式:DOCX
- 页数:20
- 大小:232.25KB
Java案例指导书之水果管理系统.docx
《Java案例指导书之水果管理系统.docx》由会员分享,可在线阅读,更多相关《Java案例指导书之水果管理系统.docx(20页珍藏版)》请在冰点文库上搜索。
Java案例指导书之水果管理系统
水果管理系统—案例指导书
一、课程设计目的
1.能够利用面向对象程序设计思想和Java编程技术按照特定软件设计要求开发小型应用程序;
2.能够针对实际的软件工程复杂问题,采用面向对象的建模技术,完成软件工程问题解决方案的设计;
3.能够根据软件复杂工程问题的设计需要,开发合理的软件模块,对工程中的设计进行检验和评价;
4.具有良好的代码表达习惯,能够与业界同行及社会公众进行准确、高效的沟通和交流。
二、课程设计任务
1.设计一个图形界面(GUI)的水果管理应用程序,完成简单的添加、查询、删除等功能,并通过此次课程设计来检测和提升自己的Java编程能力!
2.系统简要说明:
编写系统主界面,如图1所示。
界面使用边界布局管理器,在北部显示一张图片;在西部显示一个菜单导航,在中部作为主显示区,默认显示“欢迎使用水果系统”字样,当点击该导航栏按钮时,主显示区显示相应的界面。
图1系统主界面
图2添加界面
图3查看界面
提示:
信息的读写可以使用对象流。
三、课程设计环境
1.Windows7操作系统
2.JDK1.7
3.Eclipse集成开发环境
四、课程设计思路
4.1搭建程序框架
4.2MainClass.java是程序的主类,通过主类运行水果管理界面
importview.FruitManage;
publicclassMainClass{
publicstaticvoidmain(String[]args){
FruitManagemanage=newFruitManage();
manage.setVisible(true);
}
}
4.3水果管理界面FruitManage.java
packageview;
publicclassFruitManageextendsJFrameimplementsActionListener{
JPanelcenterPanel;
publicFruitManage(){
init();//窗体初始化
}
privatevoidinit(){
//1.设置窗体的属性
setIconImage(newImageIcon("title.png").getImage());
setSize(650,450);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//2.设置窗体上部的图片
JLabeltopLabel;
topLabel=newJLabel(newImageIcon("top.png"));
add(topLabel,BorderLayout.NORTH);
//3.设置导航菜单
JPanelmenuPanel;
menuPanel=newJPanel();
menuPanel.setLayout(newGridLayout(5,1));
String[]strs={"添加水果","查看水果","退出程序","",""};
JButton[]buts=newJButton[strs.length];
for(inti=0;i buts[i]=newJButton(strs[i]); buts[i].setFont(newFont("黑体",Font.BOLD,16)); menuPanel.add(buts[i]); buts[i].addActionListener(this); } buts[3].setEnabled(false);//按钮不显示 buts[4].setEnabled(false);//按钮不显示 add(menuPanel,BorderLayout.WEST); //4.设置主题内容窗格 centerPanel=newJPanel(); JLabelcenterLab=newJLabel("欢迎使用水果管理系统"); centerLab.setHorizontalAlignment(JLabel.CENTER); centerLab.setVerticalAlignment(JLabel.CENTER); centerLab.setFont(newFont("宋体",Font.BOLD,36)); centerPanel.add(centerLab); add(centerPanel,BorderLayout.CENTER); } @Override publicvoidactionPerformed(ActionEvente){ //5.设置点击导航菜单后触发的处理事件 if("添加水果".equals(e.getActionCommand())){ centerPanel.setVisible(false); centerPanel=newAddFruit(); add(centerPanel,BorderLayout.CENTER); }elseif("查看水果".equals(e.getActionCommand())){ centerPanel.setVisible(false); centerPanel=newShowFruit(); add(centerPanel,BorderLayout.CENTER); }elseif("退出程序".equals(e.getActionCommand())){ dispose(); } //更新视图 validate(); } } 4.4添加水果AddFruit.java packageview; publicclassAddFruitextendsJPanelimplementsActionListener{ //---------------------------- (1)定义组件 JLabellbId;//编号标签 JLabellbName;//名称标签 JLabellbPrice;//单价标签 JLabellbUnit;//单位标签 JLabellbType;//类别标签 JTextFieldtfId;//编号文本框 JTextFieldtfName;//名称文本框 JTextFieldtfPrice;//单价文本框 JComboBox JCheckBox[]cbType;//类别 JButtonbtnAdd;//添加按钮 publicAddFruit(){ setLayout(null); //-------------------------- (2)创建组件并加入窗口 //--------(2.1)创建组件 //------编号------ lbId=newJLabel("编号: "); lbId.setBounds(30,20,50,40); tfId=newJTextField(); tfId.setBounds(90,20,140,40); //------名称------ lbName=newJLabel("名称: "); lbName.setBounds(280,20,50,40); tfName=newJTextField(); tfName.setBounds(340,20,140,40); //------单价------ lbPrice=newJLabel("单价: "); lbPrice.setBounds(30,95,50,40); tfPrice=newJTextField(); tfPrice.setBounds(90,95,140,40); //------单位------ lbUnit=newJLabel("单位: "); lbUnit.setBounds(280,95,50,40); comboUnit=newJComboBox comboUnit.setBounds(340,95,140,40); //设置下拉列表组件(添加可选顶,设置默认选中项) comboUnit.addItem("个"); comboUnit.addItem("斤"); comboUnit.addItem("袋"); comboUnit.addItem("箱"); comboUnit.setSelectedIndex (1);//选中下标为0的项(第1项) //------类别------ lbType=newJLabel("类别: "); lbType.setBounds(30,170,50,40); cbType=newJCheckBox[3]; cbType[0]=newJCheckBox("低温保存"); cbType[1]=newJCheckBox("干燥保存"); cbType[2]=newJCheckBox("短期保存"); cbType[0].setBounds(90,170,85,40); cbType[1].setBounds(90+95,170,85,40); cbType[2].setBounds(90+95*2,170,85,40); //------添加按钮------ btnAdd=newJButton("添加"); btnAdd.setBounds(200,245,100,40); btnAdd.addActionListener(this); //修改字体 Fontf=newFont("宋体",Font.BOLD,14); lbId.setFont(f); tfId.setFont(f); lbName.setFont(f); tfName.setFont(f); lbPrice.setFont(f); tfPrice.setFont(f); lbUnit.setFont(f); comboUnit.setFont(f); lbType.setFont(f); cbType[0].setFont(f); cbType[1].setFont(f); cbType[2].setFont(f); btnAdd.setFont(f); //--------(2.2)加入窗口 //把组件添加到面板中 add(lbId); add(tfId); add(lbName); add(tfName); add(lbPrice); add(tfPrice); add(lbUnit); add(comboUnit); add(lbType); for(inti=0;i add(cbType[i]); } add(btnAdd); } @Override publicvoidactionPerformed(ActionEvente){ //---1.判断必填信息是否完整 //判断编号是否不空 Stringid=tfId.getText(); if("".equals(id)){ JOptionPane.showMessageDialog(this,"编号不能为空! ", "错误提示",JOptionPane.ERROR_MESSAGE); return; } //判断价格是否为数字 intprice=0; try{ price=Integer.parseInt(tfPrice.getText()); }catch(Exceptione1){ JOptionPane.showMessageDialog(this,"价格应为数字! ", "错误提示",JOptionPane.ERROR_MESSAGE); return; } //---2.封装成Fruit对象 Fruitf=newFruit(); //编号 f.setId(id); //名称 f.setName(tfName.getText()); //价格 f.setPrice(price); //单位 Stringunit=comboUnit.getSelectedItem().toString(); f.setUnit(unit); //类别 StringBufferbuffer=newStringBuffer(); for(JCheckBoxcb: cbType){ //如果按钮被选中,则把文字加入字符串 if(cb.isSelected()){ buffer.append(cb.getText()+"&"); } } //如果字符缓冲区不空,则删除最后一个字符 if(buffer.length()>0){ buffer.deleteCharAt(buffer.length()-1); } Stringtype=buffer.toString(); f.setType(type); //---3.保存水果信息 booleanb=FruitDao.addFruit(f); if(b==true){ JOptionPane.showMessageDialog(this,"保存水果信息成功! "); tfName.setText(null); tfPrice.setText(null);//单价文本框 }else{ JOptionPane.showMessageDialog(this,"编号重复,水果信息未保存! ", "错误提示",JOptionPane.ERROR_MESSAGE); } } } 4.5数据Fruit类 packagebean; publicclassFruitimplementsSerializable{ privateStringid;//编号 privateStringname;//名称 privatedoubleprice;//价格 privateStringunit;//单位 privateStringtype;//类别 //添加水果类的空参构造方法及全参构造方法 //添加所有属性的get和set方法 } 4.6查询显示水果ShowFruit.java packageview; publicclassShowFruitsextendsJPanel{ JTabletable=null; JButtonbtnDel=null; //表格标题 String[]title={"水果编号","名称","价格","单位","类别"}; //获取水果信息(水果信息的列数应该与表格标题的数量相同) String[][]data=FruitDao.getFruits(); publicShowFruits(){ //1.构造表格对象并显示 // (1)创建表格 //table=newJTable(6,5); table=newJTable(data,title); // (2)设置表格参数 table.getColumnModel().getColumn(4).setPreferredWidth(200);//设置第5列宽度为200 table.getTableHeader().setReorderingAllowed(false);//不可改变列顺序 table.getTableHeader().setResizingAllowed(false);//不可改变列宽 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只能选一行 //(3)创建滚动面板并把表格加到该面板中 JScrollPanejs=newJScrollPane(table); js.setPreferredSize(newDimension(500,240));//设置滚动面板的宽度、高度。 需要在使用布局管理器的时候使用 //(4)把滚动面板加入到窗口 add(js); //2.构造删除按钮并显示 // (1)创建按钮 btnDel=newJButton("删除"); // (2)通过匿名内部类为按钮添加事件处理 btnDel.addActionListener(newActionListener(){ @Override publicvoidactionPerformed(ActionEvente){ //确认删除的提示 // (1)弹出提示框 intc=JOptionPane.showConfirmDialog(ShowFruits.this,"确认删除? ", "确认对话框",JOptionPane.YES_NO_OPTION); // (2)若用户没有选择是,则结束函数的执行。 if(c! =JOptionPane.YES_OPTION){ return; } //获取编号 // (1)找到表格中被选中的行号(从0开始) introw=table.getSelectedRow(); if(row==-1) row=0; // (2)根据行号找到水果编号(位于该行的首列) Stringid=data[row][0]; //如果编号不为空,则进行删除 if(! "".equals(id)){ FruitDao.delFruit(id); JOptionPane.showMessageDialog(ShowFruits.this,"删除成功! "); } //重新显示表格 // (1)获取新数据 data=FruitDao.getFruits(); // (2)构造新的表格模型(用于管理表格数据的对象) DefaultTableModeltableModel=newDefaultTableModel(data,title); //(3)替换原来的表格模型(更新数据的显示) table.setModel(tableModel); //(4)重新调整最后一列的宽度 table.getColumnModel().getColumn(4).setPreferredWidth(200);//设置第5列宽度为200 } }); //(3)把按钮加入到窗口 add(btnDel); } } 4.7关于水果的操作FruitDao类 packagedao; publicclassFruitDao{ //存放水果信息的数据结构(整个程序中查看、添加、修改和删除都应针对此list对象进行操作) privatestaticList //用于保存数据的文件名 privatestaticStringfilename="fruits.dat"; //用于初始化的静态代码块,其作用是找到保存数据的文件并从中读入水果信息到list(仅在类加载时执行一次) static{ Filefile=newFile(filename); //如果文件存在,才能进行初始化 if(file.exists()){ //使用对象输入流读入信息(该信息须由对象输出流保存) FileInputStreamfis=null; ObjectInputStreamois=null; try{ //对象输入流的参数是InputStream对象,不能直接打开文件,因此先创建FileInputStream fis=newFileInputStream(filename); ois=newObjectInputStream(fis); //从文件中读入水果数据(该数据是以链表形式存储的) list=(ArrayList }catch(Exceptione){ JOptionPane.showMessageDialog(null,"读取水果数据文件错误! 请检查文件! "); e.printStackTrace(); }finally{ try{ if(ois! =null)ois.close(); }catch(IOExceptione){ e.printStackTrace(); } } } } //查询所有的水果数据,并以二维数组的形式返回 publicstaticString[][]getFruits(){ String[][]data=newString[list.size()][5]; introw=0;//当前行 Iterator while(it.hasNext())
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 案例 指导书 水果 管理 系统