uibutton positioning when resize uifigure

28 次查看(过去 30 天)
If I create a simple popup window like this:
h = uifigure; h.Position = [0 0 600 400]; centerfig(h)
uiLabel = uilabel(h,'Position',[30 360 540 30],'Text',{'Blah blah ...'});
uiOK = uibutton(h,'Position',[510 30 60 20],'Text','OK');
uiCancel = uibutton(h,'Position',[400 30 90 20],'Text','Cancel');
uit = uitable(h,'Data',rand(10,4),'Position',[30 90 540 250]);
It looks fine. And if I maximise the figure then the table resizes nicely and the label retains its position relative to the figure left. But the OK and Cancel buttons behave differently to each other.
The Cancel button retains its position relative to the figure left, which is what I expected, while the OK button retains its position relative to the figure right leaving a big gap in-between. The OK button behaviour might be desirable (and if they both did this it would save me having to do a resize function!), but I don't understand why the two buttons behave differently. Can someone explain?
  2 个评论
Rik
Rik 2021-8-20
You could consider setting the Units property to Normalized. That way it will take up a fraction of your window, regardless of window size.
Justin
Justin 2021-8-20
Thanks. I can do a workaround easily enough though, I was more interested in explanation of why behaviour is different.

请先登录,再进行评论。

采纳的回答

Himanshu Jain
Himanshu Jain 2021-8-23
As I understand, you have tried to align the 'OK' button with the right side of the UITable, so while resizing, matlab tries to hold that alignment.
When you maximize the figure window, the size of the table adjusts itself which is as expected and as the button is aligned with the right part of the table, the 'OK' button is shifted towards right.
If we talk about the 'Cancel' button, it is not aligned with any other component in the figure so it retains it's Position property even after maximizing the window.
To check the above statement, you can try to change the Position of the 'OK' button so that it does not align with the table. For example, you can execute the below code and try maximizing the window.
h = uifigure; h.Position = [0 0 600 400]; centerfig(h)
uiLabel = uilabel(h,'Position',[30 360 540 30],'Text',{'Blah blah ...'});
uiOK = uibutton(h,'Position',[510 30 50 20],'Text','OK');
uiCancel = uibutton(h,'Position',[400 30 90 20],'Text','Cancel');
uit = uitable(h,'Data',rand(10,4),'Position',[30 90 540 250]);
You will notice that now both the buttons retain their Position property even after maximizing the window.
  1 个评论
Justin
Justin 2021-8-23
Thanks for the explanation. It's potentially useful feature that if two components are right aligned, by chance or design, they'll retain that on resize. I couldn't find any mention of this in the documentation for uifigure/uibutton.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by