1、114234226徐洪飚 JAVA程序设计语言 课程实验报告课程题目:相册薄 专业班级:地理信息系统112班 姓 名 : 徐洪飚 学 号 :114234226指导老师:阿尔达克 2013/11/14一、 实验前言: 几十年来,计算机一直作为一个交流平台被广泛使用。尽管 email可能仍然是基于计算机进行交互的最常用媒介,但是,近年来社交网络应用(诸如 Facebook、Qrkut、MySpace、人人网等)却受到巨大的热捧。基于这样一种背景,你在本次作业中的任务是创建一个应用程序,用于了解一个简单的社交网络。什么是社交网络? 可能你还不是很熟悉社交网络,社交网络最简单的定义就是:了解一群人及他
2、们彼此之间关系的一种方式。二、实验内容:本次作业,你将创建一个应用程序,用于了解一个简单社交网络中的信息。说的更专业一些,在这个社交网络,你的应用程序将允许添加用户简介,删除用户简介,查询用户简介。另外,对于每一个简介,你可以把一个人的名字和他的简介联系在一起,一个可选的图片用于显示这个人的外貌信息,一个可选的“当前状态”(通常是一个字符串,描述简介的主人当前正在从事的活动),和一组朋友列表。三、实验目的:利用所学习的java知识,创建应用程序并完成功能相应完整的程序设计。四、程序源代码:/* * File: FacePamphlet.java * - *当它被完成,这一计划将实施基本的社会网
3、络 * 管理系统。 */一:FacePamphletimport acm.program.*;import acm.graphics.*;import acm.util.*;import java.awt.event.*;import javax.swing.*;public class FacePamphlet extends Program implements FacePamphletConstants /public class FacePamphlet extends ConsoleProgram implements FacePamphletConstants /* 该方法具有用于
4、初始化的责任*团团员中的应用,并照顾其他任何*初始化,需要执行。*/ public void init() /初始化窗口 initComponent(); /监听器 addActionListeners(); / * *这个类是负责检测按钮时 *点击或交互使用,所以你必须添加代码 *以应对这些行动。 */ public void actionPerformed(ActionEvent e) /姓名文本框中的内容 name = nameField.getText().trim(); boolean isInclude = userData.containsProfile(name); /添加对象
5、 if(e.getSource() = addButton) if(name.equals() canvas.showMessage(用户姓名不能为空!); else if(isInclude) canvas.showMessage(name+已存在!); elseuserProfile = new FacePamphletProfile(name);/创建新用户的数据文件 userData.addProfile(userProfile);/加入HashMap canvas.displayProfile(userData.getProfile(name);/在界面上显示 canvas.show
6、Message(新用户 +name+ 已创建!); /删除对象 if(e.getSource() = deleteButton) if(isInclude) userProfile = userData.getProfile(name); FacePamphletProfile friendProfile; if(userProfile.getFriends() != null) while(userProfile.getFriends().hasNext()String friendName = userProfile.getFriends().next();/遍历当前用户的好友列表 fri
7、endProfile = userData.getProfile(friendName); friendProfile.removeFriend(name); userData.addProfile(friendProfile);/覆盖同名键 userProfile.removeFriend(friendName); userData.addProfile(userProfile);if(userProfile.getFriends() = null) break;/如果当前用户好友列表被清空,跳出循环 userData.deleteProfile(name); canvas.displayP
8、rofile(null); canvas.showMessage( + name + 已删除!); else canvas.showMessage( + name + 不存在!); /查找目标对象 if(e.getSource() = findButton) /判断是否存在该用户 if(isInclude) userProfile = userData.getProfile(name); canvas.displayProfile(userProfile); canvas.showMessage(显示 + name + 用户信息); else canvas.displayProfile(nul
9、l); canvas.showMessage( + name + 数据不存在!); /更改状态 if(e.getSource() = statusButton) changeStatus(name,isInclude); /更改图片 if(e.getSource() = pictureButton) changePhoto(name,isInclude); /添加好友 if(e.getSource() = friendButton) addFriend(name,isInclude); / 键盘监听处理程序 public void keyTyped(KeyEvent e) boolean is
10、Include = userData.containsProfile(name); if(isInclude) if(e.getSource()=statusField)&(e.getKeyChar()=n) changeStatus(name,true); if(e.getSource()=pictureField)&(e.getKeyChar()=n) changePhoto(name,true); if(e.getSource()=friendField)&(e.getKeyChar()=n) addFriend(name,true); /窗口布局初始化 private void ini
11、tComponent() / You fill this in add(new JLabel(姓名),NORTH); nameField = new JTextField(TEXT_FIELD_SIZE); add(nameField,NORTH); addButton = new JButton(添加); add(addButton,NORTH); deleteButton = new JButton(删除); add(deleteButton,NORTH); findButton = new JButton(查询); add(findButton,NORTH); statusField =
12、 new JTextField(TEXT_FIELD_SIZE); statusField.addKeyListener(this);/监听状态框 add(statusField,WEST); statusButton = new JButton(改变状态); add(statusButton,WEST); add(new JLabel(EMPTY_LABEL_TEXT),WEST); pictureField = new JTextField(TEXT_FIELD_SIZE); pictureField.addKeyListener(this); /监听图片框 add(pictureFiel
13、d,WEST); pictureButton = new JButton(改变照片); add(pictureButton,WEST); add(new JLabel(EMPTY_LABEL_TEXT),WEST); friendField = new JTextField(TEXT_FIELD_SIZE); friendField.addKeyListener(this); /监听好友框 add(friendField,WEST); friendButton = new JButton(添加好友); add(friendButton,WEST); /新建画布 canvas = new FacePamphletCanvas(); add(canvas); /* * 更新用户的状态 * param name 用户名 * param isContain 用户是否存在 */ private void changeStatus(String name,boolean isContain) if (isContain) userProfile=userData.getProfile(name); userProfile.setStatus(statusField.getText(); userData.addProfil
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1