这个程序是我第一次接触MATLAB 的GUI设计写的,我能感受到里面有少许bug,然后有少数功能我没有去定义,然后由于我这个工具后来又写了很多,集成了很多其他的功能,比如说字典,数据库等等,所以比较注意封装成单独的函数,以后可以慢慢介绍。
从这个工具可以了解到各种图标设计,回调函数设计,定时器使用等等,需要提升的地方就是封装性还不够好,有很多的全局变量,我们在写程序的时候一定要尽量使用全局变量,这是个很不好的习惯。
效果:
这个图标你们要自己选一张图片命名为’logo.jpg’,顺带说一句,这个录屏工具也是用MATLAB写的。
figFrame=get(GUI.calculator,'JavaFrame'); % 设置图标
newIcon=javax.swing.ImageIcon('logo.jpg');
figFrame.setFigureIcon(newIcon);
function calculator()
warning off;
close all; clear cll;
global GUI
GUI.calculator=figure('units','pixels','numbertitle','off',...
'name','Calculator','menubar','none',...
'position',[900 200 250 315],'visible','on',...
'color',[217/256 228/256 241/256]);
GUI.dtime=uicontrol('parent',GUI.calculator,'style','text',...
'String','','position',[40 270 190 30],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',8,...
'fontsize',12);
GUI.button_dot=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','.','position',[105 10 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_dot);
GUI.button_add=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','+','position',[151 10 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_add);
GUI.button_equ=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','=','position',[197 10 41 71],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_equ);
GUI.button_sub=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','-','position',[151 48 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_sub);
GUI.button_mul=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','*','position',[151 86 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_mul);
GUI.button_x=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','1/x','position',[197 86 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_x);
GUI.button_div=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','/','position',[151 124 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_div);
GUI.button_per=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','%','position',[197 124 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12);
GUI.button_back=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'string','←','position',[10 162 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_back);
GUI.button_del=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'string','CE','position',[59 162 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_del);
GUI.button_clr=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'string','C','position',[105 162 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_clc);
GUI.button_ver=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'string','±','position',[151 162 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_ver);
GUI.button_sqrt=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'string','√','position',[197 162 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback_sqrt);
GUI.button0=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','0','position',[10 10 90 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback0);
GUI.button1=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','1','position',[10 48 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback1);
GUI.button2=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','2','position',[59 48 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback2);
GUI.button3=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','3','position',[105 48 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback3);
GUI.button4=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','4','position',[10 86 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback4);
GUI.button5=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','5','position',[59 86 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback5);
GUI.button6=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','6','position',[105 86 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback6);
GUI.button7=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','7','position',[10 124 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback7);
GUI.button8=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','8','position',[59 124 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback8);
GUI.button9=uicontrol('parent',GUI.calculator,'style','pushbutton',...
'String','9','position',[105 124 41 33],...
'backgroundcolor',[231/256 237/256 245/256],...
'foregroundcolor',[0 0 0],'fontsize',12,...
'callback',@callback9);
GUI.Edit=uicontrol('parent',GUI.calculator,'Style','edit',...
'string','0','Position',[10 200 228 66],...
'fontsize',18,'horizontalalignmen','right');
try
figFrame=get(GUI.calculator,'JavaFrame'); % 设置图标
newIcon=javax.swing.ImageIcon('logo.jpg');
figFrame.setFigureIcon(newIcon);
end
GUI.timer=timer('StartDelay',1,'TimerFcn',@cdisptime,'Period',1,...
'ExecutionMode','fixedRate');
start(GUI.timer);
end
function callback0(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
first =str2num(a0);
else
set(GUI.Edit,'string',[a0 '0']);
a0=[a0 '0'];
first=str2num(a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '0']);
len=length(first)+1;
a0( 1:1:len)=[];
a0=[a0 '0'];
second=str2num(a0);
flag=0;
end
end
function callback1(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '1';
set(GUI.Edit,'string',a0);
first=str2num(a0);
else
a0=[a0 '1'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '1']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '1'];
second=str2num(a0);
flag=0;
end
end
function callback2(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '2';
set(GUI.Edit,'string',a0);
first=2;
else
a0=[a0 '2'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '2']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '2'];
second=str2num(a0);
flag=0;
end
end
function callback3(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '3';first=3;
set(GUI.Edit,'string',a0);
else
a0=[a0 '3'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '3']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '3'];
second=str2num(a0);
flag=0;
end
end
function callback4(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '4';first=4;
set(GUI.Edit,'string',a0);
else
a0=[a0 '4'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '4']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '4'];
second=str2num(a0);
flag=0;
end
end
function callback5(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '5';first=5;
set(GUI.Edit,'string',a0);
else
a0=[a0 '5'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '5']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '5'];
second=str2num(a0);
flag=0;
end
end
function callback6(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '6';first=6;
set(GUI.Edit,'string',a0);
else
a0=[a0 '6'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '6']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '6'];
second=str2num(a0);
flag=0;
end
end
function callback7(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '7';first=7;
set(GUI.Edit,'string',a0);
else
a0=[a0 '7'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '7']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '7'];
second=str2num(a0);
flag=0;
end
end
function callback8(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '8';first=8;
set(GUI.Edit,'string',a0);
else
a0=[a0 '8'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '8']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '8'];
second=str2num(a0);
flag=0;
end
end
function callback9(~,~)
global GUI
global first
global second
global symbol
global flag
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0= '9';first=9;
set(GUI.Edit,'string',a0);
else
a0=[a0 '9'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '9']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '9'];
second=str2num(a0);
flag=0;
end
end
function callback_dot(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
if flag1==1
return
end
flag1=1;
a0=get(GUI.Edit,'string');
if flag==0
if a0=='0'
a0=[a0 '.'];
first=0;
set(GUI.Edit,'string',a0);
else
a0=[a0 '.'];
first=str2num(a0);
set(GUI.Edit,'string',a0);
end
else
if second==[]
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '0.']);
second=0;
else
a0=get(GUI.Edit,'string');
set(GUI.Edit,'string',[a0 '.']);
len=length(first)+1;
a0(1:1:len)=[];
a0=[a0 '.'];
second1=str2num(a0);
if second1==[]
else
second=second1;
end
end
end
end
function callback_add(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
if flag==1
return
end
flag=1;
flag1=0;
a0=get(GUI.Edit,'string');
a0=[a0 '+'];
set(GUI.Edit,'string',a0);
end
function callback_equ(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
flag1=0;
second=[];
flag=0;
a0=get(GUI.Edit,'string');
answer=eval(a0);
first=answer;
set(GUI.Edit,'string',num2str(first));
first=[];
end
function callback_sub(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
if flag==1
return
end
flag=1;
flag1=0;
a0=get(GUI.Edit,'string');
a0=[a0 '-'];
set(GUI.Edit,'string',a0);
end
function callback_mul(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
if flag==1
return
end
flag=1;
flag1=0;
a0=get(GUI.Edit,'string');
a0=[a0 '*'];
set(GUI.Edit,'string',a0);
end
function callback_x(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
callback_equ();
a0=get(GUI.Edit,'string');
answer=1/str2num(a0);
set(GUI.Edit,'string',num2str(answer));
end
function callback_div(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
if flag==1
return
end
flag=1;
flag1=0;
a0=get(GUI.Edit,'string');
a0=[a0 '/'];
set(GUI.Edit,'string',a0);
end
function callback_clc(~,~)
global first; first=[];
global second; second=[];
global symbol; symbol=[];
global flag; flag=0;
global flag1;flag1=0;
global flag2;flag2=1;
global GUI
set(GUI.Edit,'string','0');
end
function callback_sqrt(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
callback_equ();
a0=get(GUI.Edit,'string');
first=sqrt(str2num(a0));
set(GUI.Edit,'string',num2str(first));
end
function callback_ver(~,~)
global GUI
global first
global second
global symbol
global flag
callback_equ();
a0=get(GUI.Edit,'string');
first=-str2num(a0);
set(GUI.Edit,'string',num2str(first));
end
function callback_back(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
a0=get(GUI.Edit,'string');
len=length(a0);
if len==1
first=[];
flag=1;
set(GUI.Edit,'string','0')
end
if str2num(a0(len-1))
aa=0;
else
aa=1;
end
a=(str2num(a0(len-1))-0)>10|(str2num(a0(len-1))-0)<-1
if aa==1
a=1;
end
if a
flag=1;
else
flag=0;
end
len=length(a0);
a0(len)=[];
set(GUI.Edit,'string',a0);
end
function callback_del(~,~)
global GUI
global first
global second
global symbol
global flag
global flag1
while 1
a0=get(GUI.Edit,'string');
len=length(a0);
if len==1
first=[];
flag=1;
a0='0';
set(GUI.Edit,'string',a0);
return
end
if a0(len)~='+'&a0(len)~='-'&a0(len)~='/'&a0(len)~='*'
a0(len)=[];
set(GUI.Edit,'string',a0)
else
flag=1;
return
end
end
end
function cdisptime(~,~)
global GUI
set(GUI.dtime,'String',datestr(now)); % 将edit控件的内容改成当前时间
end