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

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

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

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

文件或仓库网址:

以下为核心源码:

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$')));
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 <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')

效果如下:

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

最后祝大家,生活愉快!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值