




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验报告单院(系) 计算机学院 专业 计算机科学与技术班级 计科2班 姓名 学号 同组人 无 实验室 S4305 组号 日期 课程 Java程序设计 指导教师 成绩 实验项目编号 8103200405 实验项目名称 图形用户界面编程 一、 实验目的熟悉并掌握Java窗体和组件的定义,事件的定义。二、 实验环境Windows XP 操作系统 Eclipse三、 实验原理1、熟悉Swing的基本组件,包括文本输入框、多行文本输入框、按钮、列表框等;2、熟悉常用的布局管理器3、了解GUI图像用户界面的设计方法4、掌握Java组件的事件处理机制5、熟悉基于内部类和匿名类的事件处理方式四、 算法设计【任务一】在实验二Building House的基础上,添加界面。如下图所示。五、 主要代码及说明【任务一】Location类:abstract class Locationprotected String name;protected Location exits;protected String description;public Location(String name) = name;protected String getDescription()description = 我站在+name+,我看到如下过道:;for (int i=0;iexits.length; i+)description += ;if(i!=exits.length-1)description += ,;description += .;return description;Room类: class Room extends Locationprotected String decoration;public Room(String name, String decoration)super(name);this.decoration = decoration;public String getDescription()description = super.getDescription()+ 我看到的装饰是:+decoration;return description;RoomWithDoor类:public class RoomWithDoor extends Room implements IHasExteriorDoor private String doorDescription;private Location doorLocation;public RoomWithDoor(String name, String decoration, String doorDescription)super(name,decoration);this.doorDescription=doorDescription;public String getDescription()description = super.getDescription()+我看到门:+getDoorDescription();return description;public String getDoorDescription()return doorDescription;public void setDoorLocation(Location doorLocation)this.doorLocation=doorLocation;public Location getDoorLocation() return doorLocation;class Outside extends Locationprivate boolean isHot;public Outside(String name,boolean isHot)super(name);this.isHot = isHot;public String getDescription()if (isHot)description = super.getDescription()+ 这里非常热。;return description;Outside类:OutsideWithDoor类public class OutsideWithDoor extends Outside implements IHasExteriorDoorprivate String doorDescription;private Location doorLocation;public OutsideWithDoor(String name, boolean isHot,String doorDescription) super(name, isHot);this.doorDescription = doorDescription;public String getDescription() description = super.getDescription()+我看到门:+getDoorDescription();return description;public void setDoorLocation(Location doorLocation)this.doorLocation=doorLocation;public Location getDoorLocation() return doorLocation;public String getDoorDescription()return doorDescription;接口:IHasExteriorDoorpublic interface IHasExteriorDoor String getDoorDescription();void setDoorLocation(Location doorLocation);Location getDoorLocation();import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.*;public class HouseFrame extends JFrame private Location currentLocation=null;RoomWithDoor livingRoom, kitchen;Room diningRoom;OutsideWithDoor frontYard,backYard;Outside garden;public HouseFrame()HouseInit();setTitle(Building House Demo);Container c = getContentPane();c.setLayout(new GridLayout(2,1,20,20);JPanel p1 = new JPanel();final JTextArea cur_Description = new JTextArea(50,50);cur_Description.setFont(new Font(Default,Font.PLAIN,20);cur_Description.setLineWrap(true);cur_Description.setText(currentLocation.getDescription();Dimension preferredSize = new Dimension(200,50);/设置尺寸JPanel p2 = new JPanel(new FlowLayout();JButton jbGoHere=new JButton(Go Here);jbGoHere.setPreferredSize(preferredSize);HouseFrame类:int exitsCount = currentLocation.exits.length;String exits_currentLocation = new StringexitsCount;for (int i=0;iexitsCount;i+)exits_currentLocationi = currentL;final JComboBox jCombo = new JComboBox(exits_currentLocation);jCombo.setPreferredSize(preferredSize);final JButton jbGoThrough = new JButton(Go Through The Door);preferredSize = new Dimension(400,50);jbGoThrough.setPreferredSize(preferredSize);jbGoHere.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String selectedLocationName = jCombo.getSelectedItem().toString(); if(selectedLocationName.equals(Living Room) HouseFrame.this.currentLocation = livingRoom; if(selectedLocationName.equals(Dining Room) HouseFrame.this.currentLocation = diningRoom; if(selectedLocationName.equals(Kitchen) HouseFrame.this.currentLocation = kitchen; if(selectedLocationName.equals(Front Yard) HouseFrame.this.currentLocation = frontYard; if(selectedLocationName.equals(Back Yard) HouseFrame.this.currentLocation = backYard; if(selectedLocationName.equals(Garden) HouseFrame.this.currentLocation = garden; String tmpString = HouseFrame.this.currentLocation.getDescription();cur_Description.setText(HouseFrame.this.currentLocation.getDescription(); jCombo.removeAllItems(); for(int i=0;icurrentLocation.exits.length;i+) jCombo.addItem(currentL); if(HouseFrame.this.currentLocation instanceof IHasExteriorDoor)jbGoThrough.setVisible(true); else jbGoThrough.setVisible(false); );jbGoThrough.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(HouseFrame.this.currentLocation.exits0 instanceof IHasExteriorDoor) currentLocation=currentLocation.exits0;String tmpString = HouseFrame.this.currentLocation.getDescription();cur_Description.setText(HouseFrame.this.currentLocation.getDescription();jCombo.removeAll();String str=new String2;str0=currentL;jCombo.addItem(str0);else if(HouseFrame.this.currentLocation.exits1 instanceof IHasExteriorDoor)currentLocation=currentLocation.exits1;String tmpString = HouseFrame.this.currentLocation.getDescription();cur_Description.setText(HouseFrame.this.currentLocation.getDescription();jCombo.removeAll();String str=new String2;str0=currentL;jCombo.addItem(str1););p1.add(cur_Description);p2.add(jbGoHere);p2.add(jCombo);p2.add(jbGoThrough);c.add(p1);c.add(p2);setSize(800,400);setVisible(true);void HouseInit() livingRoom = new RoomWithDoor(Living Room, An antique carpet,An Aok Door With a Brass Knob); diningRoom = new Room(Dining Room, A crystal chandelier); kitchen = new RoomWithDoor(Kitchen, Stainless steel appliance,A Screen Door); frontYard = new OutsideWithDoor(Front Yard, false,An Aok Door With a Brass Knob); backYard = new OutsideWithDoor(Back Yard, true,A Screen Door); garden = new Outside(Garden, false); livingRoom.exits = new LocationdiningRoom,frontYard; diningRoom.exits = new LocationlivingRoom,kitchen; kitchen.exits = new LocationdiningRoom ,backYard; frontYard .exits = new LocationlivingRoom,garden; backYard.exits = new Locationgarden,kitchen; garden.exits = new LocationfrontYard,backYard; livingRoom.setDoorLocation(frontYard); frontYard.setDoorLocation(livingRoom); kitchen.setDoorLocation(backYard); backYard .setDoorLocation(kitchen); this.currentLocation = livingRoom;public static void main(String args)new HouseFrame();BuildingHouseDemo类:public class BuildingHouseDemo public static Location currentLocation;public static void BuildingHouseDemo(String args) Room livingRoom,diningRoom, kitchen; Outside frontYard,backYard,garden; livingRoom = new Room(Living Room, An antique carpet); diningRoom = ne
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论