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

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

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

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

文件或仓库网址:

以下为核心源码:

<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>

效果如下:

image

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

最后祝大家,生活愉快!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容