uibutton positioning when resize uifigure

29 vues (au cours des 30 derniers jours)
Justin
Justin le 20 Août 2021
Commenté : Justin le 23 Août 2021
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 commentaires
Rik
Rik le 20 Août 2021
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 le 20 Août 2021
Thanks. I can do a workaround easily enough though, I was more interested in explanation of why behaviour is different.

Connectez-vous pour commenter.

Réponse acceptée

Himanshu Jain
Himanshu Jain le 23 Août 2021
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 commentaire
Justin
Justin le 23 Août 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by