【发布时间】:2015-12-21 21:53:54
【问题描述】:
我尝试以编程方式创建 GUI。它应显示以下内容:
右侧的复选框是静态的!左侧复选框的数量取决于用户输入!
我尝试做的是为右侧的动态复选框创建一个面板,并为这些面板创建一个滚动条。
不幸的是,我对编程 GUI 创建完全陌生。到目前为止,使用 GUIDE 一切正常。
我找到了一个很好的例子
Adding scroll bar in subplots within GUI
用轴进入它,但我没有让它适应复选框:( 这是我迄今为止的尝试。
%# create figure, panel, and slider
w = 600; h = 500; %# width/height of figure
handles.hFig = figure('Menubar','figure', 'Resize','off', ...
'Units','pixels', 'Position',[200 200 w h]);
handles.hPan = uipanel('Parent',handles.hFig, ...
'Units','pixels', 'Position',[0 0 w-20 h]);
handles.hSld = uicontrol('Parent',handles.hFig, ...
'Style','slider', 'Enable','off', ...
'Units','pixels', 'Position',[w-20 0 20 h], ...
'Min',0-eps, 'Max',0, 'Value',0, ...
'Callback',{@onSlide,handles.hPan});
%# add checkbox
hcb = zeros(7,1);
clr = lines(7);
for i=1:7
hcb(i) = addcheckbox(handles);
pause(1) %# slow down so that we can see the updates
end
滑块功能没有改变。这是对我来说最有问题的功能:
function hcb = addcheckbox(handles)
%# look for checkboxes
cb = findobj(handles.hPan, 'type','checkbox');
if isempty(cb)
%# create first checkbox
hcb = uicontrol(handles.hFig,'Style','checkbox',...
'String','Display file extension',...
'Value',1,'Position',[30 20 130 20]);
else
%# get height of figure
p = get(handles.hFig, 'Position');
h = p(4);
%# increase panel height, and shift it to show new space
p = get(handles.hPan, 'Position');
set(handles.hPan, 'Position',[p(1) p(2)-h p(3) p(4)+h])
% %# compute position of new axis: append on top (y-shifted)
% p = get(ax, 'Position');
% if iscell(p), p = cell2mat(p); end
% p = [p(1,1) max(p(:,2))+h p(1,3) p(1,4)];
%
% %# create the new axis
% hAx = axes('Parent',handles.hPan, ...
% 'Units','pixels', 'Position',p);
%
% %# adjust slider, and call its callback function
% mx = get(handles.hSld, 'Max');
% set(handles.hSld, 'Max',mx+h, 'Min',0, 'Enable','on')
% %#set(handles.hSld, 'Value',mx+h) %# scroll to new space
% hgfeval(get(handles.hSld,'Callback'), handles.hSld, []);
end
end
当我执行它时,我得到了错误:
Error using findobj
Invalid handle
Error in addcheckbox (line 3)
cb = findobj(handles.hPan, 'type','checkbox');
这个错误是有道理的,因为我没有一个名为 checkboxes 的句柄...在示例中,'axes' 是图中包含的预定义句柄...我如何在此上下文中集成复选框?
【问题讨论】:
-
提供的代码不会复制所描述的错误。如果您的 GUIDE GUI 可以正常运行,为什么要制作程序化 GUI?
-
哦,你是对的!有没有可能编辑这个问题?我错过了考虑的事情... GUIDE 如何处理复选框的动态创建?必须预先定义相应的数字还是我错了?
-
是的,没错。但是,我建议的另一种选择是使用
uitable,您可以在 GUIDE 中实现它并且可以包含复选框。它还为您处理滚动。我个人更喜欢程序化 GUI,但这里没有必要重做整个 GUI。 -
非常感谢您! uitable 似乎提供了我需要的一切!
标签: matlab user-interface checkbox scrollbar figure