欢迎来到天天文库
浏览记录
ID:55686382
大小:77.50 KB
页数:7页
时间:2020-05-24
《云大设计模式实验3(装饰者模式).doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、云南大学软件学院实验报告课程:设计模式学期:2012-2013学年第一学期任课教师:陆歌浩专业:软件工程学号:姓名:孙金文成绩:实验3装饰者模式一、实验目的1.熟悉装饰者模式2.运用装饰者模式编制简单的java程序二、实验内容类图:实验代码:packageheadfirst.decorator;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Panel;importjava.awt.Toolkit;importjava
2、.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassEatFood{publicstaticvoidmain(String[]args){FoodFramefoodFrame=newFoodFrame();fo
3、odFrame.setSize(400,450);foodFrame.setLocation(FoodFrame.src.width/2-foodFrame.getWidth()/2,FoodFrame.src.height/2-foodFrame.getHeight()/2);foodFrame.setVisible(true);}}classFoodFrameextendsJFrameimplementsActionListener{staticDimensionsrc=Toolkit.getDefaultToolkit().getScreenS
4、ize();Restaurantfood;JComboBoxcomboBox;JButton[]seasoning;JLabelfoodName,flavouring,price;String[]tiaoliao={"鸡肉(2元)","牛肉(2.5元)","杂酱(1.5元)","酱油(免费)","醋(免费)","辣椒(免费)"};String[]str={"粗米线(6元)","细米线(7元)","面条(6元)"};publicFoodFrame(){super("米线店结账系统(孙金文)");this.setDefaultCloseOperation
5、(JFrame.EXIT_ON_CLOSE);setFoodWindow();}privatevoidsetFoodWindow(){Containerct=this.getContentPane();ct.setLayout(newGridLayout(6,1));Panelp1=newPanel();p1.add(newJLabel("主食:"));comboBox=newJComboBox(str);p1.add(comboBox);comboBox.addActionListener(this);ct.add(p1);ct.add(newJL
6、abel("调料选择:"));food=newThinRiceNoodle();seasoning=newJButton[tiaoliao.length];JPaneljp2=newJPanel();for(inti=0;i7、nothing"));ct.add(price=newJLabel("总价格为:"+food.cost()));}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(comboBox)){if(comboBox.getSelectedItem().equals("细米线(7元)")){food=newThinRiceNoodle();}elseif(comboBox.getSelectedItem().equals("粗米线(6元)")){food=newThickRiceN8、oodle();}elseif(comboBox.getSelectedItem().equals("面条(
7、nothing"));ct.add(price=newJLabel("总价格为:"+food.cost()));}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(comboBox)){if(comboBox.getSelectedItem().equals("细米线(7元)")){food=newThinRiceNoodle();}elseif(comboBox.getSelectedItem().equals("粗米线(6元)")){food=newThickRiceN
8、oodle();}elseif(comboBox.getSelectedItem().equals("面条(
此文档下载收益归作者所有