Components

Now that we've set the dimensions of the cells, let's create some components and add them to the containers.

Define some sample components—buttons whose labels correspond to where they'll be added in the grid layout.

>>> tl = JButton("Top Left") >>> tr = JButton("Top Right") >>> bl = JButton("Bottom Left") >>> br = JButton("Bottom Right")

Add the components to the contentPane using their corresponding GridBagConstraints.

>>> contentPane.add(bl, bottom_left)

>>> contentPane.add(br, bottom_right)

Size the frame and show it.

>>> frame.size=(400,200) >>> frame.validate() >>> frame.visible=1

If you've been following along, your frame should look like the one in Figure 14-30. Now change the size of the container to 600,100.

Figure 14-30. GridBag.py Example

_ nix!

Top Left

Top Right

Bottom Left

Bottom Right

»> frame . size= ( 600, 100 ) »> frame .validate ()

Notice that the 80:20 ratio between the top left and top right widths remains consistent. So does the 50:50 ratio between the top left and bottom left heights. Your frame should look like the one in Figure 14-31.

Figure 14-31. GridBag.py Example with a Container Size of 600,100

TG|t Lett

lop FUQt*

Bottom Lett

Bottom Kiijlit

Let's change the size to 500,500 so you can get the true feel of things.

>>> frame.size=(500,500) >>> frame.invalidate() >>> frame.validate()

Try packing the frame. What do you think will liappen?

Was this article helpful?

0 0

Post a comment