首页 > 其他分享 >MATLAB GUI设计——菜单选项中加入图标

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

时间:2022-10-10 15:00:19浏览次数:94  
标签:tmp License GUI .% version MATLAB setMenuIcon menuStack 图标

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这个仓库,具体玩一玩。

最后祝大家,生活愉快!

标签:tmp,License,GUI,.%,version,MATLAB,setMenuIcon,menuStack,图标
From: https://www.cnblogs.com/qpwz/p/16775663.html

相关文章

  • 利用MVC设计模式构建GUI(PyQt5版)
    今天介绍一个PyQt5中利用MVC设计模式构建GUI的例子,这个案例来源于《MATLAB面向对象编程——从入门到设计模式(第2版)》第7章内容,关于存取款的GUI工具设计,详情请参考127~160页......
  • MATLAB 风格指南 2.0
    MATLAB风格指南2.0 变量从小写开始用混合大小写命名变量名具有大作用域的变量应该具有有意义的命名,作用域小的变量可以有简短的命名用前缀*n*来表示对象数量......
  • 用Matlab搞了个小工具,以后给你女朋友的证件照换底色再也不用发愁了
     证件照换底色这件事在生活中并不是很频繁,然而,当你手上只有一种底色的照片,遇到找工作,填表或者在线上传某种要求必须为指定底色的证件照时,却又是比较重要的。我想更重要......
  • MATLAB|GetColor 命令行版本
     最近在后台收到私信,一些朋友说要是GetColor工具能提供接口供使用的话就好了。于是,这款命令行版本的GetColor诞生了......对GetColor工具感兴趣的朋友们可以查看:......
  • Matlab R2022b 到底香不香?
     11时隔半年,又到了更新Matlab的季节了......R2022b看着挺香的:2345看下我们所重点关注的AppBuilding和DeepLearningToolbox:AppBuilding87DeepLearnin......
  • 数组——继计算方法与matlab原理,
    稀疏矩阵以结构体数组存储(C语言也有结构体数组)phase1:三元组:basis,翻转,+I等单操作,按行读取,要遍历整个数组,typedefstructTriple{ //三元组存储非零元信息,数组下......
  • SAP GUI 修改轻松访问 easy access的背景图片
    0x01SAPgui提供了丰富的界面风格设定功能,跟Windows桌面主题一样,可以任意设定自己喜欢的颜色风格。不过美中不足,它的背景图片却比较单调,天天看惯了正式系统的蔚蓝水池或......
  • SAP GUI 更改登录界面显示信息
    0x01简单说明在SAPGUI的登录界面,左部输入登录信息如客户端、用户名、密码等,右部空余部分可维护一些登录信息文本,如登录的产品、客户端说明及注意事项等,此项操作详见SAP......
  • easygui库
    根据需求,EasyGui在buttonbox()上建立了一系列的函数供调用。1、msgBox()msgbox(msg='(Yourmessagegoeshere)',title='',ok_button='OK',image=None,root=None)m......
  • 想要字体图标设计师却给了SVG?没关系,自己转
    本文为Varlet组件库源码主题阅读系列第三篇,读完本篇,你可以了解到如何将svg图标转换成字体图标文件,以及如何设计一个简洁的Vue图标组件。Varlet提供了一些常用的图标,图标......