GUI中添加图标,能很大程度上提高用户的友好度,显得格外舒适。
MATLAB 官方并没有提供向菜单栏中添加图标的直接方法,基于figure
设计的GUI中,可以通过JaveFrame
属性间接设置图标。基于uifigure
设计的GUI目前还没有间接的方法,相信后续版本会解决这个问题吧。
今天介绍一个在Matlab File Exchange/Github
上公开的很实用的工具: setMenuIcon,我们可以很方便的向菜单选项中添加图标了。
文件或仓库网址:
Matlab File Exchange: setMenuIcon - File Exchange - MATLAB Central (mathworks.com)
以下为核心源码:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="matlab" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.8rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: var(--codeboxes); position: relative !important; border-radius: 0.3rem; color: rgb(255, 255, 255); padding: 8px 1.5rem 6px 0px; margin-bottom: 1.5rem; margin-top: 1.5rem; width: inherit; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> 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 https://www.gnu.org/licenses/.
% 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/pre>)));
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));
</pre>
示例代码:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="matlab" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.8rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: var(--codeboxes); position: relative !important; border-radius: 0.3rem; color: rgb(255, 255, 255); padding: 8px 1.5rem 6px 0px; margin-bottom: 1.5rem; margin-top: 1.5rem; width: inherit; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> % 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 https://www.gnu.org/licenses/.
%% 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')
</pre>
效果如下:
今天的分享就到这里了,感兴趣的朋友们可以clone这个仓库,具体玩一玩。
最后祝大家,生活愉快!