首页 第五讲 GUI设计

第五讲 GUI设计

举报
开通vip

第五讲 GUI设计nullnull第五讲 图形用户界面(GUI) 设计null图形用户界面(GUI) 设计 GUI的基本图形对象: 用户界面控件对象 (uicontrol) 建立如按钮、滚动条、弹出式菜单以及文本框等对象 用户界面菜单对象 (uimenu) 在图形窗口中产生下拉式菜单和子菜单null菜单设计 控件设计 用户界面设计工具5.1 菜单设计5.1 菜单设计一、 用户菜单的建立 建立一级菜单项的函数调用形式为: 主菜单对象句柄=uimenu(图形窗口句柄,属性名1,属性值1,属性名2,属性值2...

第五讲 GUI设计
nullnull第五讲 图形用户界面(GUI) 设计 领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计 null图形用户界面(GUI) 设计 GUI的基本图形对象: 用户界面控件对象 (uicontrol) 建立如按钮、滚动条、弹出式菜单以及文本框等对象 用户界面菜单对象 (uimenu) 在图形窗口中产生下拉式菜单和子菜单null菜单设计 控件设计 用户界面设计工具5.1 菜单设计5.1 菜单设计一、 用户菜单的建立 建立一级菜单项的函数调用形式为: 主菜单对象句柄=uimenu(图形窗口句柄,属性名1,属性值1,属性名2,属性值2,…) plotopt=uimenu(gcf,'label','plotoptions') 建立子菜单项的函数调用形式为: 子菜单对象句柄=uimenu(父菜单对象句柄,属性名1,属性值1,属性名2,属性值2,…) plotlt=uimenu(plotopt,'Label','Line Types'); plotsym=uimenu(plotopt,'Label','Plot Symbol', 'Separator', 'on' ); plotcol=uimenu(plotopt,'Label','Plot color','Separator','on');null例5.1 建立“图形演示系统”菜单。菜单条中含有3个菜单项:Plot、Option和Quit。Plot中有Sine Wave和Cosine Wave两个子菜单项,分别控制在本图形窗口画出正弦和余弦曲线。Option菜单项的内容包括:Grid on和Grid off控制给坐标轴加网格线,Box on和Box off控制给坐标轴加边框,而且这4项只有在画有曲线时才是可选的,Figure Color控制图形窗口背景颜色。Quit控制是否退出系统。nullscreen=get(0,'ScreenSize'); W=screen(3);H=screen(4); figure('Color',[1,1,1],'Position',[0.2*H,0.2*H,0.6*W,0.4*H],... 'Name','图形演示系统','NumberTitle','off','MenuBar','none'); %定义Plot菜单项 hplot=uimenu(gcf,'Label','&Plot'); uimenu(hplot,'Label','Sine Wave','Call',['t=-pi:pi/20:pi;','plot(t,sin(t));',... 'set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''on'');',... 'set(hbon,''Enable'',''on'');','set(hboff,''Enable'',''on'');']); uimenu(hplot,'Label','Cosine Wave','Call',['t=-pi:pi/20:pi;','plot(t,cos(t));',... 'set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''on'');',... 'set(hbon,''Enable'',''on'');','set(hboff,''Enable'',''on'');']); %定义Option菜单项 hoption=uimenu(gcf,'Label','&Option'); hgon=uimenu(hoption,'Label','&Grig on','Call','grid on','Enable','off'); hgoff=uimenu(hoption,'Label','&Grig off','Call','grid off','Enable','off'); hbon=uimenu(hoption,'Label','&Box on','separator','on','Call','box on','Enable','off'); hboff=uimenu(hoption,'Label','&Box off','Call','box off','Enable','off'); hfigcor=uimenu(hoption,'Label','&Figure Color','Separator','on'); uimenu(hfigcor,'Label','&Red','Accelerator','r','Call','set(gcf,''Color'',''r'');'); uimenu(hfigcor,'Label','&Blue','Accelerator','b','Call','set(gcf,''Color'',''b'');'); uimenu(hfigcor,'Label','&Yellow','Call','set(gcf,''Color'',''y'');'); uimenu(hfigcor,'Label','&White','Call','set(gcf,''Color'',''w'');'); %定义Quit菜单项 uimenu(gcf,'Label','&Quit','Call','close(gcf)');null二 快捷菜单 在MATLAB中,可以使用uicontextmenu函数和图形对象的UIContextMenu属性来建立快捷菜单,具体步骤为: (1)利用uicontextmenu函数建立快捷菜单。 (2)利用uimenu函数为快捷菜单建立菜单项。 (3)利用set函数将该快捷菜单和某图形对象联系起来。 例5.2 绘制曲线y=2e-0.5xsin(2πx),并建立一个与之相联系的快捷菜单,用以控制曲线的线型和曲线宽度。 x=0:pi/100:2*pi; y=2*exp(-0.5*x).*sin(2*pi*x); hl=plot(x,y); hc=uicontextmenu; %建立快捷菜单 hls=uimenu(hc,'Label','线型'); %建立菜单项 hlw=uimenu(hc,'Label','线宽'); uimenu(hls,'Label','虚线','Call','set(hl,''LineStyle'','':'');'); uimenu(hls,'Label','实线','Call','set(hl,''LineStyle'',''-'');'); uimenu(hlw,'Label','加宽','Call','set(hl,''LineWidth'',2);'); uimenu(hlw,'Label','变细','Call','set(hl,''LineWidth'',0.5);'); set(hl,'UIContextMenu',hc); %将该快捷菜单和曲线对象联系起来5.2 控件设计5.2 控件设计5.2.1 控件 (1)按钮(Push Button)。 (2)切换(按下/弹起)按钮(Toggle Button)。 (3)单选按钮(Radio Button)。 (4)复选框(Check Box)。 (5)列 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 框(List Box)。 (6)弹出框(Popup Menu)。 (7)编辑框(Edit Box)。 (8)滑动条(Slider)。 (9)静态文本(Static Text)。 (10)边框(Frame)。 5.2.2 对话框的设计 1. 建立控件对象 MATLAB提供了用于建立控件对象的函数uicontrol,其调用格式为: 对象句柄=uicontrol(图形窗口句柄,属性名1,属性值1,属性名2,属性值2,…) 其中各个属性名及可取的值和前面介绍的uimenu函数相似,但也不尽相同,下面将介绍一些常用的属性。 2. 控件对象的属性 MATLAB的10种控件对象使用相同的属性类型,但是这些属性对于不同类型的控件对象,其含义不尽相同。除Children、Parent、Tag、Type、UserData、Visible等公共属性外,还有一些常用的特殊属性。null例5.3 建立数制转换对话框。在左边输入一个十进制整数和2~16之间的数,单击“转换”按钮能在右边得到十进制数所对应的2~16进制字符串,单击“退出”按钮退出对话框。 nullhf=figure('Color',[0,1,1],'Position',[100,200,400,200],... 'Name','数制转换','NumberTitle','off','MenuBar','none'); uicontrol(hf,'Style','Text', 'Units','normalized',... 'Position',[0.05,0.8,0.45,0.1],'Horizontal','center',... 'String','输 入 框','Back',[0,1,1]); uicontrol(hf,'Style','Text','Position',[0.5,0.8,0.45,0.1],... 'Units','normalized','Horizontal','center',... 'String','输 出 框','Back',[0,1,1]); uicontrol(hf,'Style','Frame','Position',[0.04,0.33,0.45,0.45],... 'Units','normalized','Back',[1,1,0]); uicontrol(hf,'Style','Text','Position',[0.05,0.6,0.25,0.1],... 'Units','normalized','Horizontal','center',... 'String','十进制数','Back',[1,1,0]); uicontrol(hf,'Style','Text','Position',[0.05,0.4,0.25,0.1],... 'Units','normalized','Horizontal','center',... 'String','2~16进制','Back',[1,1,0]); he1=uicontrol(hf,'Style','Edit','Position',[0.25,0.6,0.2,0.1],... 'Units','normalized','Back',[0,1,0]); he2=uicontrol(hf,'Style','Edit','Position',[0.25,0.4,0.2,0.1],... 'Units','normalized','Back',[0,1,0]); uicontrol(hf,'Style','Frame','Position',[0.52,0.33,0.45,0.45],... 'Units','normalized','Back',[1,1,0]); ht=uicontrol(hf,'Style','Text','Position',[0.6,0.5,0.3,0.1],... 'Units','normalized','Horizontal','center','Back',[0,1,0]); COMM=['n=str2num(get(he1,''String''));','b=str2num(get(he2,''String''));',... 'dec=trdec(n,b);','set(ht,''string'',dec);']; uicontrol(hf,'Style','Push','Position',[0.18,0.1,0.2,0.12],... 'String','转 换','Units','normalized','Call',COMM); uicontrol(hf,'Style','Push','Position',[0.65,0.1,0.2,0.12],... 'String','退 出','Units','normalized','Call','close(hf)');null程序调用了trdec.m函数文件,该函数的作用是将任意十进制整数转换为2~16进制字符串。trdec.m函数文件如下: function dec=trdec(n,b) ch1='0123456789ABCDEF'; %十六进制的16个符号 k=1; while n~=0 %不断除某进制基数取余直到商为0 p(k)=rem(n,b); n=fix(n/b); k=k+1; end k=k-1; strdec=''; while k>=1 %形成某进制数的字符串 kb=p(k); strdec=strcat(strdec,ch1(kb+1:kb+1)); k=k-1; end dec=strdec;null例5.4 建立图形演示对话框。在编辑框输入绘图命令,单击“绘图”按钮能在左边坐标轴得到所对应的图形,弹出框提供色图控制,列表框提供坐标网格线和坐标边框控制。 nullclf; set(gcf,'Unit','normalized','Position',[0.2,0.3,0.65,0.35]); set(gcf,'Menubar','none','Name','图形演示','NumberTitle','off'); axes('Position',[0.05,0.15,0.55,0.7]); uicontrol(gcf,'Style','text', 'Unit','normalized',... 'Posi',[0.63,0.85,0.2,0.1],'String','输入绘图命令','Horizontal','center'); hedit=uicontrol(gcf,'Style','edit','Unit','normalized','Posi',[0.63,0.15,0.2,0.68],... 'Max',2); %Max取2,使Max-Min>1,从而允许多行输入 hpopup=uicontrol(gcf,'Style','popup','Unit','normalized',... 'Posi',[0.85,0.8,0.15,0.15],'String','Spring|Summer|Autumn|Winter'); hlist=uicontrol(gcf,'Style','list','Unit','normalized',... 'Posi',[0.85,0.55,0.15,0.25],'String','Grid on|Grid off|Box on|Box off'); hpush1=uicontrol(gcf,'Style','push','Unit','normalized',... 'Posi',[0.85,0.35,0.15,0.15],'String','绘 图'); uicontrol(gcf,'Style','push','Unit','normalized',... 'Posi',[0.85,0.15,0.15,0.15],'String','关 闭','Call','close all'); set(hpush1,'Call','COMM(hedit,hpopup,hlist)'); set(hlist,'Call','COMM(hedit,hpopup,hlist)'); set(hpopup,'Call','COMM(hedit,hpopup,hlist)'); %COMM.m函数文件: function COMM(hedit,hpopup,hlist) com=get(hedit,'String'); n1=get(hpopup,'Value'); n2=get(hlist,'Value'); if ~isempty(com) %编辑框输入非空时 eval(com'); %执行从编辑框输入的命令 chpop={'spring','summer','autumn','winter'}; chlist={'grid on','grid off','box on','box off'}; colormap(eval(chpop{n1})); eval(chlist{n2}); end null5.3 用户界面设计工具nullThe Layout Editor with a blank GUI template nulldrag a push button into the layout area nullGUI FIG-Files and M-Files GUIDE stores a GUI in two files, which are generated the first time you save or run the GUI: A FIG-file, with extension .fig, which contains a complete description of the GUI layout and the components of the GUI: push buttons, menus, axes, and so on. A M-file, with extension .m, that contains the code that controls the GUI, including the callbacks for its components. These two files correspond to the tasks of laying out and programming the GUI. When you lay out of the GUI in the Layout Editor, your work is stored in the FIG-file. When you program the GUI, your work is stored in the M-fileCreating Graphical User Interfaces Creating Graphical User Interfaces Designing the GUI Laying Out the GUI Setting Properties for GUI Components Programming the GUI Saving and Running a GUIDesigning the GUI Designing the GUI An axes that displays plot of the data Three push buttons that enable you to select the type of plot you want. The pop-up menu contains three strings -- peaks, membrane, and sinc, which correspond to MATLAB functions. You can select the data to plot from this menu. Laying Out the GUILaying Out the GUI Open a New GUI in the Layout Editor Set the GUI Figure Size. Add the Components Align the Components. null>> guidenullSelect the Blank GUI (default) template. Click OK null若要显示组件面板(component palette)的GUI组件的名称, 选择 File → Preferences→ Show names in component palettenullSet the GUI Figure Sizenull If you want to set the position or size of the GUI to an exact value: 1. Select Property Inspector from the View menu. 2. Select the button next to Units and then select inches from the pop-up menu 3. Click the + sign next to Position. 4. Type the x and y coordinates of the point where you want the lower left corner of the GUI to appear, and its width and height, as shown in the following figure. 5. Reset the Units property to characters.nullnullAdd the Components1. Add the panel and push buttons to the GUI.nullAdd the remaining components to the GUI: a static text , a pop-up menu, an axes nullAlign the ComponentsAlignment Tool : 将有同一父代的组件对齐 如:将上面的三个push buttons对齐 pressing Ctrl and clicking them. Tools menu → Align Objects ,出现 Alignment Object. 本例中采用如下设置: 20 pixels spacing between push buttons in the vertical direction. Left-aligned in the horizontal direction. nullnullSetting Properties for GUI ComponentsView →Property Inspector from the menu to display the Property Inspector dialog box Name Property Title Property String Property for Push Buttons and Static Text String Property for Pop-Up Menus Callback Properties The Tag Property null Name Property null Title Property String Property for Push Buttons and Static Text null String Property for Pop-up Menusnullthe current layout of the GUI appears as :null Callback Properties Components use callbacks to do their work. Each component and menu item has properties that specify its callbacks. When you create a GUI, you must program the callbacks you need to control operation of the GUI The Tag Property The Tag property provides a string as a unique identifier for each component. GUIDE uses this identifier to construct unique callback names for the different components in the GUInullWhen you save or run the GUI, GUIDE sets the name of the callback subfunction in the pop-up menu Callback property to plot_popup_Callback. Programming the GUIProgramming the GUICreating the GUI M-File Opening the GUI M-File Sharing Data Between Callbacks Adding Code to the Opening Function Adding Code to the Callbacks Using the Object Browser to Identify Callbacks Opening the GUI M-FileOpening the GUI M-FileAdd code to the callbacks for the three push buttons and the pop-up menu. open the created M-file by clicking the M-file Editor icon on the toolbar. In the editor, move the cursor to a specific callback by clicking the function icon on the toolbar, then selecting the callback you want in the pop-up menu that displays.nullnullnullSharing Data Between Callbacks Storing the data in the MATLAB handles structure. All components in a GUI share the same handles structure. Eg. Store vector X in the handles structure: 1. Choose a name for the field of the handles structure where you want to store the data, for example, handles.my_data 2. Add the field to the handles structure: handles.my_data = X; 3. Save the handles structure with the guidata function: guidata(hObject,handles) hObject is the handle to the component object that executes the callback. hObject and handles are input arguments for all the callbacks generated by GUIDEnullAdding Code to the Opening Function The Opening Function: is the first callback in every GUI M-file. Name: GUI M-文件的名字_OpeningFcn,例如: simple_gui_OpeningFcn Eg. Add code that creates three data sets in the opening function, using peaks, membrane, and sinc. null% --- Executes just before simple_gui is made visible. function simple_gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled (see VARARGIN) % Create the data to plot handles.peaks=peaks(35); handles.membrane=membrane; [x,y] = meshgrid(-8:.5:8); r = sqrt(x.^2+y.^2) + eps; sinc = sin(r)./r; handles.sinc = sinc; handles.current_data = handles.peaks; surf(handles.current_data)Add this codeAutogenerated codenullnullPush Button CallbacksAdding Code to the CallbacksnullPush Button Callbacks% --- Executes on button press in surf_pushbutton. function surf_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to surf_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Display surf plot of the currently selected data surf(handles.current_data); Add this codeAutogenerated codenullAdd this code to the Mesh push button callback: % Display mesh plot of the currently selected data mesh(handles.current_data); Add this code to the Contour push button callback: % Display contour plot of the currently selected data contour(handles.current_data);nullPop-up Menu Callback% --- Executes on selection change in plot_popup. function plot_popup_Callback(hObject, eventdata, handles) % hObject handle to plot_popup (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val = get(hObject,'Value'); str = get(hObject, 'String'); switch str{val}; case 'peaks' % User selects peaks handles.current_data = handles.peaks; case 'membrane' % User selects membrane handles.current_data = handles.membrane; case 'sinc' % User selects sinc handles.current_data = handles.sinc; end guidata(hObject,handles)Add this codeAutogenerated codenullUsing the Object Browser to Identify Callbacks In a complicated GUI, keeping track of callbacks can be difficult. To identify the component corresponding to a callback, select Object Browser from the View menu. The Object Browser lists the tag and string properties of each component of the GUI.nullUsing the Object Browser to Identify Callbacks
本文档为【第五讲 GUI设计】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_224454
暂无简介~
格式:ppt
大小:629KB
软件:PowerPoint
页数:0
分类:互联网
上传时间:2012-12-25
浏览量:80