資源描述:
《云大設(shè)計(jì)模式實(shí)驗(yàn)3(裝飾者模式).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、云南大學(xué)軟件學(xué)院實(shí)驗(yàn)報(bào)告課程:設(shè)計(jì)模式學(xué)期:2012-2013學(xué)年第一學(xué)期任課教師:陸歌浩專(zhuān)業(yè):軟件工程學(xué)號(hào):姓名:孫金文成績(jī):實(shí)驗(yàn)3裝飾者模式一、實(shí)驗(yàn)?zāi)康?.熟悉裝飾者模式2.運(yùn)用裝飾者模式編制簡(jiǎn)單的java程序二、實(shí)驗(yàn)內(nèi)容類(lèi)圖:實(shí)驗(yàn)代碼: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元)","醬油(免費(fèi))","醋(免費(fèi))","辣椒(免費(fèi))"};String[]str={"粗米線(6元)","細(xì)米線(7元)","面條(6元)"};publicFoodFrame(){super("米線店結(jié)賬系統(tǒng)(孫金文)");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("調(diào)料選擇:"));food=newThinRiceNoodle();seasoning=newJButton[tiaoliao.length];JPaneljp2=newJPanel();for(inti=0;i7、nothing"));ct.add(price=newJLabel("總價(jià)格為:"+food.cost()));}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(comboBox)){if(comboBox.getSelectedItem().equals("細(xì)米線(7元)")){food=newThinRiceNoodle();}elseif(comboBox.getSelectedItem().equals("粗米線(6元)")){food=newThickRiceN
8、oodle();}elseif(comboBox.getSelectedItem().equals("面條(