09-06-2011 08:53 AM
This is closer but still not exactly right. It might work in your specific case (with LabelFields) but is risky otherwise.
One problem is here:
layoutChild(field, field.getPreferredWidth(), field.getPreferredHeight());
The field doesn't have to tell you its exact requirements. Besides, you might not be able to satisfy them. Think of your screen "real estate" as a budget - you can only give this much, but if they need less, that's better! On the other hand, the field does have to comply with the dimensions you give it - it should not exceed the width and height you are giving it.
So something like
layoutChild(field, width/2, height);
is much closer to what you need. Compare that to what Peter wrote a couple of replies ago.
Now, having called layoutChild, you have access to the field's actual width and height. You no longer need to rely on its preferred sizes! field.getWidth() and field.getHeight() give you the field's size. You can take the maximum of your two fields heights and set it as your Manager's actual height. Moreover, if you switch the order of your layoutChild and setPositionChild calls, setPositionChild can access your field's actual width to, say, justify it to the right properly.
Good luck, you are on the right track!
09-06-2011 08:55 AM
Your code assumes that
a) Both Fields will take up less than 1/2 of the width of the Screen
b) Both Fields are the same height
c) There are two Fields in the Manager
I suspect these are safe assumptions for you to make for your code and usage, but you might need to be careful in future uses.
I hope you feel comfortable writing a more complicated manager for your next exercise.
09-07-2011 06:38 PM