下面是一個快速但完整的示例。代碼被註釋掉,易於遵循:
function MyImageViewer
% prepare GUI
hFig = figure('Menubar','none', 'Name','Image Viewer');
hPan(1) = uipanel('Parent',hFig, 'Position',[0 0 0.3 1]);
hPan(2) = uipanel('Parent',hFig, 'Position',[0.3 0 0.7 1]);
ax = axes('Parent',hPan(2), 'Units','normalized', 'Position',[0 0 1 1]);
axis(ax, 'off', 'image')
[jtree,htree] = uitree('v0', 'Parent',hFig, ...
'Root','C:\Users\Amro\Pictures\', 'SelectionChangeFcn',@changeFcn);
set(htree, 'Parent',hPan(1), 'Units','normalized', 'Position',[0 0 1 1]);
jtree.expand(jtree.getRoot); % expand root node
% list of supported image extensions
fmt = imformats;
imgExt = lower([fmt.ext]);
function changeFcn(~,~)
% get selected node
nodes = jtree.getSelectedNodes;
if isempty(nodes), return; end
n = nodes(1);
% only consider a leaf node (skip folders)
if ~n.isLeaf, return; end
% get complete node path (absolute filename)
p = arrayfun(@(nd) char(nd.getName), n.getPath, 'Uniform',false);
p = strjoin(p(:).', filesep);
% check for supported image types, and show image
[~,~,ext] = fileparts(p);
if any(strcmpi(ext(2:end),imgExt))
imshow(p, 'Parent',ax)
end
end
end
請註明你被卡住究竟在何處? – georg 2014-08-29 08:53:19
我想爲樹元素實現鼠標按鍵監聽器。例如,在我上面的圖形用戶界面中,當用戶右鍵單擊aaa(1).jpg時,它會執行一些操作,就像我在右側面板中顯示的那樣。 – 2014-08-29 10:51:31
但問題是什麼?這裏沒有提供關於你的代碼的細節。如何編碼你的treeview?問題是,如何訪問樹元素?或者如何實現'ButtonDownFcn'回調或...? – georg 2014-08-29 11:37:38