MATLAB 自制簡單計算器 GUI

這個程序是我第一次接觸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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章