>>> frame.contentPane.add(Button("Button")) is equivalent to doing this:
>>> button = Button("Button") >>> frame.contentPane.add(button)
The first way cre ates an anonym ous instance of Button and adds it to the frame, which is okay since we don't need acce ss to the button component for this example.
Only the last componenl added tF the frame shows ug, Wliy? The abswer lies in tha defauh layout manager for Frame—BorderLayout—which lays out components along the borders. I won't po moo detail about BorderLa yout here (site Chapter 14 for that). Suffice it to say that we need to change to another manager, FlowLayo ut, which arranges the components from left to right as long as there's room.
I'll tell you more about FlowLayout later. For now, we'll tmport ime FlowLayout c lass from the java.awt package and set the frame's layout property to a FlowLayout instance. Then we'll pack the frame, that is, invoke the layout manager.
>>> from java.awt import FlowLayout
>>> frame.contentPane.layout = FlowLayout()
Now all of the added components are visMe.
Was this article helpful?