(一)学习总结
首先理解图形用户界面应用程序(1)首先应该有主函数应用程序,以开头,是一切程序的前提 import test.WelcomeFrame; public class MainApp { /** * 项目运行 */public static void main(String[] args) { new WelcomeFrame().setVisible(true);}} (2)初始化数据,将用户界面所需数据储存,初始化数据,初始化窗口信息 import java.util.ArrayList; import keke.Pet; /** * 存储数据 */public class DataBase {public static ArrayListdata = new ArrayList ();//初始数据static{ data.add(new Pet("1","哮天犬","3","1",5000.00)); data.add(new Pet("2","奔波而把","2","2",10000.00)); data.add(new Pet("3","把博尔奔","1","1",5500.00)); data.add(new Pet("4","诸葛亮","2","2",2000.00)); data.add(new Pet("5","李白","3","2",5000.00)); data.add(new Pet("6","泥鳅怪","2","1",8000.00)); }} (3)当然要添加管理员访问类,从而对初始化后的程序进行获取,添加,删除等操作 import java.util.ArrayList; import data.DataBase; import keke.Pet;/** * 管理员数据访问类 */ public class AdminDao {//获取所有数据public ArrayList queryAllData(){ return DataBase.data;}//添加数据public void addPet(Pet pet){ DataBase.data.add(pet);}//删除数据public void delPet(String delNo){ for(int i = 0; i < DataBase.data.size();i++){ Pet thisPet = DataBase.data.get(i); if(thisPet.getNo().equalsIgnoreCase(delNo)){ DataBase.data.remove(i); } }} } (4)定义初始化的数据 package keke; public class Pet {private String No;private String Name;private String Number;private String Age;private Double Cost;public Pet() { }public Pet(String No,String Name,String Number,String Age, Double Cost){ super(); this.No=No; this.Name=Name; this.Number=Number; this.Age=Age; this.Cost=Cost;}public String getNo() { return No;}public void setNo(String no) { this.No = no;}public String getName() { return Name;}public void setName(String name) { this.Name = name;}public String getNumber() { return Number;}public void setColor(String number) { this.Number = number;}public String getAge() { return Age;}public void setAge(String age) { this.Age = age;} public Double getCost() { return Cost;}public void setCost( Double cost) { this.Cost = cost;}public String toString(){ return "宠物的编号为:"+this.No+"宠物的名称为:"+this.Name+"宠物的年龄为:"+this.Age+"宠物的数量为:"+this.Number+"宠物的价格为:"+this.Cost;}} (5)定义服务类,对界面中添加 删除 修改等应用进行写算法定义/** * 管理员服务类*/public class AdminService {private AdminDao adminDao = new AdminDao();//查询服务public ArrayList queryPet(){ //调用Dao层的方法获取所有数据 ArrayList data = adminDao.queryAllData(); return data; }//添加服务public boolean addPet(String no,String name,String age,String number,String cost){ ArrayList data = queryPet(); Iterator iterator = data.iterator(); while(iterator.hasNext()){ Pet pet = iterator.next(); //如果存在重复编号则添加不成功 if(pet.getNo().equals(no)){ return false; } } Pet thisPet = new Pet(no,name,age,number,Double.parseDouble(cost)); adminDao.addPet(thisPet); //添加成功 return true;}//修改服务public boolean updatePet(String no,String name,String age,String number,String cost){ ArrayList data = queryPet(); for(int i = 0; i < data.size();i++){ Pet pet = data.get(i); if(pet.getNo().equals(no)){ adminDao.delPet(no); Pet thisPet = new Pet(no,name,age,number,Double.parseDouble(cost)); adminDao.addPet(thisPet); return true; } } return false;}//删除服务public boolean delPet(String no){ ArrayList data = queryPet(); for(int i = 0; i < data.size();i++){ Pet pet = data.get(i); if(pet.getNo().equals(no)){ adminDao.delPet(no); return true; } } return false;}
}
(6)书写test类 ,构造所需方法public AdminDialog(Frame owner,boolean modal){ super(owner,modal); this.init(); //初始化 this.addComponent(); //添加组件 this.addListener(); //添加监听器 queryPet(); //创建对象时显示数据}添加监听器,当需要调用应用包的时候会自动调用运行声明import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import service.AdminService; import tools.GUITools; import keke.Pet; 对按钮,界面需定义位置:如 addNoText.setBounds(50, 280, 80, 25); addNameText.setBounds(150, 280, 80, 25); addAgeText.setBounds(250, 280, 80, 25); addNumberText.setBounds(350, 280, 80, 25); addCostText.setBounds(450, 280, 80, 25); (二)实验总结实验内容:基本要求:使用GUI,编写一个宠物商店管理系统实现对宠物信息的统一管理。假设宠物种类有猫、狗,宠物信息包括:编号、品种、年龄、数量、价格。系统启动进入登录界面,登录成功进入管理界面,否则提出错误信息后退出系统。在管理界面实现以下功能:(1)显示所有宠物列表(2)添加宠物到列表(3)删除宠物(4)对宠物信息进行修改 (5)退出 具体操作步骤上述已述 操作截图
码云托管
git@git.oschina.net:qq2546914025/jike1501hanmengshiyan7.git