龙空技术网

MATLAB GUI设计——菜单选项中加入图标

图像处理与MATLAB 127

前言:

目前各位老铁们对“matlab2020新建gui”大约比较珍视,你们都需要分析一些“matlab2020新建gui”的相关知识。那么小编也在网络上汇集了一些有关“matlab2020新建gui””的相关文章,希望你们能喜欢,小伙伴们一起来了解一下吧!

GUI中添加图标,能很大程度上提高用户的友好度,显得格外舒适。

MATLAB 官方并没有提供向菜单栏中添加图标的直接方法,基于figure设计的GUI中,可以通过JaveFrame属性间接设置图标。基于uifigure设计的GUI目前还没有间接的方法,相信后续版本会解决这个问题吧。

今天介绍一个在Matlab File Exchange/Github上公开的很实用的工具: setMenuIcon,我们可以很方便的向菜单选项中添加图标了。

文件或仓库网址:

Matlab File Exchange: setMenuIcon - File Exchange - MATLAB Central (mathworks.com)Github:

以下为核心源码:

 function setMenuIcon(menuObject,iconFile) % SETMENUICON  Add icons to UIMENU items. %   SETMENUICON(MENUOBJECT,ICONFILE) sets the icon of MENUOBJECT to %   ICONFILE. MENUOBJECT is a menu item created with MATLAB's own UIMENU. %   ICONFILE denotes the path to an image file containing the desired icon. %   This should work with GIF, JPEG and PNG images.  %   Copyright (C) 2020 Florian Rau % %   setMenuIcon is free software: you can redistribute it and/or modify it %   under the terms of the GNU General Public License as published by the %   Free Software Foundation, either version 3 of the License, or (at your %   option) any later version. % %   setMenuIcon is distributed in the hope that it will be useful, but %   WITHOUT ANY WARRANTY; without even the implied warranty of %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU %   General Public License for more details. % %   You should have received a copy of the GNU General Public License along %   with setMenuIcon.  If not, see <;.  %   REVISION HISTORY %   version 1.0.0   initial release %   version 1.0.1   corrected description and added acknowledgements %   version 1.0.2   minor clean-up of the code %   version 1.0.3   added example %   version 1.0.4   added check for App Designer figures %   version 1.0.5   minor clean-up  % validate inputs validateattributes(menuObject,{'matlab.ui.container.Menu'},{'scalar'}) validateattributes(iconFile,{'char'},{'row'}) if ~exist(iconFile,'file')     error('File not found: %s',iconFile) end  % build stack of Menu objects menuStack = menuObject; while isa(menuStack(1).Parent,'matlab.ui.container.Menu')     menuStack = [menuStack(1).Parent menuStack]; %#ok<AGROW> end  % check for App Designer figure hFigure = menuStack(1).Parent; if isempty(get(hFigure,'JavaFrame_I'))     error('Sorry, setMenuItem does not work with App Designer figures.') end  % obtain jFrame (temporarily disabling the respective warning) warnID  = 'MATLAB:ui:javaframe:PropertyToBeRemoved'; tmp     = warning('query',warnID); warning('off',warnID) jFrame  = get(hFigure,'JavaFrame'); warning(tmp.state,warnID)  % get jMenuBar tmp      = fieldnames(jFrame); tmp      = tmp(cellfun(@any,regexp(tmp,'^f(?:HG\d|Figure)Client$'))); jMenuBar = jFrame.(tmp{1}).getMenuBar;  % obtain jMenuItem positions = [menuStack.Position]; while jMenuBar.getMenuCount < positions(1)     pause(0.05) end jMenuItem = jMenuBar.getMenu(positions(1)-1); for ii = 2:numel(menuStack)     if jMenuItem.getMenuComponentCount < positions(ii)         jMenuItem.doClick;         pause(0.05)         javax.swing.MenuSelectionManager.defaultManager.clearSelectedPath;     end     tmp       = jMenuItem.getMenuComponents;     tmp       = tmp(arrayfun(@(x) contains(class(x),'JMenu'),tmp));     jMenuItem = tmp(positions(ii)); end  % add icon to jMenuItem jMenuItem.setIcon(javax.swing.ImageIcon(iconFile)); 

示例代码:

 % Copyright (C) 2020 Florian Rau % % This file is part of setMenuIcon. %  % setMenuIcon is free software: you can redistribute it and/or modify it % under the terms of the GNU General Public License as published by the % Free Software Foundation, either version 3 of the License, or (at your % option) any later version. %  % setMenuIcon is distributed in the hope that it will be useful, but % WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General % Public License for more details. %  % You should have received a copy of the GNU General Public License along % with setMenuIcon.  If not, see <;.   %% Step 1: create figure, menus and menu-items  hFigure = figure( ...                           % create figure     'Menu',         'none', ...     'NumberTitle',  'off', ...     'DockControls', 'off', ...     'Name',         'Demo');  hMenu(1) = uimenu(hFigure,'Text','Menu');       % add menu to figure hItem(1) = uimenu(hMenu(1),'Text','Item');      % add item to menu  hMenu(2) = uimenu(hMenu(1),'Text','Submenu');   % add submenu to menu hItem(2) = uimenu(hMenu(2),'Text','Boo!');      % add item to submenu   %% Step 2: set icons for menus & menu-items  setMenuIcon(hMenu(1),'example1.png') setMenuIcon(hItem(1),'example2.png') setMenuIcon(hMenu(2),'example3.png') setMenuIcon(hItem(2),'example4.png') 

效果如下:

今天的分享就到这里了,感兴趣的朋友们可以去clone这个仓库,具体玩一玩。

最后祝大家,生活愉快!

标签: #matlab2020新建gui