Hide and change location of pushbutton in GUI?

14 次查看(过去 30 天)
I'm making a GUI using GUIDE, and I was wondering how I can change the location of push buttons after other have been made not visible. Haven't found anything online yet.

采纳的回答

Rik
Rik 2019-9-29
编辑:Rik 2019-10-7
You can set the Position property of a uicontrol object to move it to any location you like inside the callback that sets the Visible property for the objects you're hiding.
Edit:
Here is a small example of how you could do something like this:
function example_GUI
f=figure;
for n=1:3
pos=[(n-1)/3 0 1/4 1];
buttons(n)=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',pos,...
'String',sprintf('Click me! (%d)',n),...
'Tag',num2str(n),...%use tag to discern the buttons in the callback
'Callback',@buttoncallback); %#ok<SAGROW>
end
%store to guidata struct
h=struct;
h.f=f;
h.buttons=buttons;
h.HasBeenClicked=false(size(h.buttons));
guidata(h.f,h)
end
function buttoncallback(hObject,eventdata) %#ok<INUSD>
h=guidata(hObject);
buttonindex=str2double(get(hObject,'Tag'));
if h.HasBeenClicked(buttonindex)
return%ignore extra click
else
h.HasBeenClicked(buttonindex)=true;
guidata(h.f,h)
end
v=1:3;v(buttonindex)=[];
%hide one button
set(h.buttons(v(1)),'Visible','off')
%'move' the other button
pos=get(h.buttons(v(2)),'Position');
pos=[pos(1:2)+pos(3:4)/3 pos(3:4)*2/3];%semi-random new position
set(h.buttons(v(2)),'Position',pos)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2013b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by