设计模式之计算器

本文介绍了利用Duilib设计并实现一个计算器应用的过程,结合设计模式,如工厂模式、组合模式和模板方法模式,实现了计算器的界面与功能。通过点击不同按键触发相应计算操作,支持键盘输入,并提供了代码下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

duilib之计算器+设计模式

           学习对duilib有一段时间了,之前做过类似华为网盘的软件,还算山寨成功了,临近学期末,老师布置了一个设计模式的项目作业,就花了4天时间用duilib来做了。感觉一般,有很多地方可以改进,但是没时间了,下一步要潜心学习cocos2d-x,以后有时间在改吧,先写个总结。

           注:5-16  版本已经更新了,增加了界面换肤更能,天气即时查询功能,暂时就不放出来了,有想法的人自己去实现吧。

界面展示:

计算器界面如下,仿照ios7界面画的




            

功能介绍:

  1. 主界面点击左上角信号图标可以关闭窗口
  2.  主界面点击设置按钮可以更改当前背景图片
  3. 主界面点击计算器进入计算器界面,之后可以进行计算操作,点击左上角信号图标可以发回主界面
  4. 计算器可以接受键盘输入,可以连续计算

界面布局源码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" standalone="yes" ?>  
  2. <Window size="359,580" caption="0,0,0,32">  
  3.     <Font name="华文细黑" size="35" bold="true" italic="false" />  
  4.     <Font name="华文细黑" size="70" bold="false" italic="false" />  
  5.     <Font name=".Lock Clock" size="73" bold="false" italic="false" />  
  6.     <Font name="新宋体" size="20" bold="true" italic="false" />  
  7.     <Font name=".Lock Clock" size="24" bold="false" italic="false" />  
  8.     <Font name="新宋体" size="17" bold="true" italic="false" />  
  9.     <VerticalLayout name="bk" width="360" height="580" bkimage="file='i4..png' dest='0,0,359,580'" bkcolor="#FF020202">  
  10.         <HorizontalLayout bordersize="3" width="360" height="130">  
  11.             <Text name="ans" float="true" pos="0,43,0,0" width="359" height="75" textpadding="2,0,2,0" textcolor="#00FFFBF0" disabledtextcolor="#FFA7A6AA" font="2" align="rightwrap" />  
  12.             <HorizontalLayout width="359" height="40">  
  13.                 <Label float="true" pos="318,2,0,0" width="40" height="32" bkimage="WhiteOnBlackShadow_BatteryCharged@2x.png" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" />  
  14.                 <Label text="99%" float="true" pos="282,6,0,0" width="39" height="22" textcolor="#00FFFFFF" disabledtextcolor="#FFA7A6AA" font="3" />  
  15.                 <Text name="time" text="11:20" float="true" pos="147,6,0,0" width="60" height="24" textpadding="2,0,2,0" textcolor="#00FFFFFF" disabledtextcolor="#FFA7A6AA" font="3" align="wrap" />  
  16.                 <Label text="BTF" float="true" pos="46,7,0,0" width="33" height="21" textcolor="#00FFFFFF" disabledtextcolor="#FFA7A6AA" font="3" />  
  17.                 <Button name="reback" float="true" pos="2,4,0,0" width="40" height="28" bkimage="WhiteOnBlackEtch_4_Bars@2x.png" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" />  
  18.             </HorizontalLayout>  
  19.         </HorizontalLayout>  
  20.         <HorizontalLayout name="cal" width="359" height="450" bkcolor="#FF7793A2"visible="false">  
  21.             <Button name="clear" float="true" pos="0,0,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='clear_normal.png' mask='0xfffffbf0'" hotimage="file='clera_hotimge.png' mask='0xfffffbf0'" pushedimage="file='clear_hot.png' mask='0xfffffbf0'" />  
  22.             <Button name="plusminos" float="true" pos="90,0,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='plusmoins_normal.png' mask='0xfffffbf0'" hotimage="file='plusmoins_hotimage.png' mask='0xfffffbf0'" pushedimage="file='plusmoins_hot.png' mask='0xfffffbf0'" />  
  23.             <Button name="peurcent" float="true" pos="180,0,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='peurcent_nomal.png' mask='0xfffffbf0'" hotimage="file='peurcent_hotimage.png' mask='0xfffffbf0'" pushedimage="file='peurcent_hoti.png' mask='0xfffffbf0'" />  
  24.             <Button name="division" float="true" pos="270,0,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='division_normla.png' mask='0xfffffbf0'" hotimage="file='division_hotimge.png' mask='0xfffffbf0'" pushedimage="file='division_hot.png' mask='0xfffffbf0'" />  
  25.             <Button name="seven" float="true" pos="0,90,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='seven_normal.png' mask='0xfffffbf0'" hotimage="file='seven_hotimage.png' mask='0xfffffbf0'" pushedimage="file='seven_hot.png' mask='0xfffffbf0'" />  
  26.             <Button name="eight" float="true" pos="90,90,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='eight_normal.png' mask='0xfffffbf0'" hotimage="file='eight_hotimage.png' mask='0xfffffbf0'" pushedimage="file='eight_hot.png' mask='0xfffffbf0'" />  
  27.             <Button name="nine" float="true" pos="180,90,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='nine_normal.png' mask='0xfffffbf0'" hotimage="file='nine_hotimage.png' mask='0xfffffbf0'" pushedimage="file='nine_hot.png' mask='0xfffffbf0'" />  
  28.             <Button name="multiplication" float="true" pos="270,90,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='multiplication_normal.png' mask='0xfffffbf0'" hotimage="file='multiplication_hot.png' mask='0xfffffbf0'" pushedimage="file='multiplication_hoti.png' mask='0xfffffbf0'" />  
  29.             <Button name="four" float="true" pos="0,180,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='four_normal.png' mask='0xfffffbf0'" hotimage="file='fout_hotimage.png' mask='0xfffffbf0'" pushedimage="file='four_hot.png' mask='0xfffffbf0'" />  
  30.             <Button name="five" float="true" pos="90,180,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='five_normal.png' mask='0xfffffbf0'" hotimage="file='five_hotimage.png' mask='0xfffffbf0'" pushedimage="file='five_hot.png' mask='0xfffffbf0'" />  
  31.             <Button name="six" float="true" pos="180,180,0,0" width="89" height="89" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="file='six_normal.png' mask='0xfffffbf0'" hotimage="file='six_hotimage.png' mask='0xfffffbf0'" pushedimage
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值