The Prototype

Now let's prototype a real application that subsets to the components the address form needs. First, though, we need to clear house.

>>> frame.visible=0 >>> frame.dispose() >>> frame = NONE >>> contentPane = NONE

Define some convenience variables for CENTER,WEST, art EAST.

>>> CENT ER=GridBagConstraints.CENTER >>> WEST=GridBagConstraints.WEST >>> EAST=GridBagConstraints . EAST

Create a 2-column-by-3-row grid thht's almost identical to the one (^eofed before except for the addition of an extra row at the bottomcallusS last ilast goes unddrnrhth bottom).

>>> top_left = makeConstraint(0,0,1,1, weightx=2 0,weighty=33, anch

>>> top_right = makeConstraint(1,0,1,1, weightx=80, anch

»> bottom_left = makeConstraint(0,1,1,1, weighty=33, anch

»> bottom_right = makeConstraint (1,1, 1, 1, anchc

>>> last = makeConstraint(1,2,1,1, weighty=33)

Define some sample components: labels, text fields, and a button.

>>> tl = JLabel("Name", JLabel.RIGHT) #Top Left component >>> tr = JTextField(20) #Top Right component

>>> bl = JLabel("eMail", JLabel.RIGHT) #Bottom Left component >>> br = JTextField(30) #Bottom Right component

Notice that we're using the s ame nomenclature as in Hie last example . (My tile components are diffemnt.

Set up the frame with a panel that uees GridBagLayout.

>>> contentPane = JPanel(layout=gbag)

>>> frame = JFrame(contentPane=contentPane)

Add the sample components to contentPane. They have the name name so we c an use Hie addCompo nents () method. Group thn components with the constraints (the OK button and the last constraint are now on the end).

>>> comps = [(tl, top_left),(tr, top_right),(bl, bottom_left),(br, bottom_right),(okay,last)]

>>> addComponents(contentPane, comps)

Size the frame, and make it visible. Your drame should look like the one in Figure 14-34.

Figure 14-34. Prototype of Name/Email Address Book Entry Form

Name eMail

Okay

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

Now let's tiy resizing the frame to see what happens "to the components. Change the frame size to 500,500.

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

Change the size to 600,100.

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

Now pack the frame, and see how good it looks. (It should look like Figure 14-35.)

Figure 14-35. A Better-Looking Version of the Name/Email Address Book Entry Form

Name[

Okay

Changeipadx and ipady for top_left. Space is added only to the first column, and there's more space between the components.

>>> top_left.ipadx, top_left.ipady = 20,20

>>> removeComponents()

>>> contentPane.add(okay, last)

Was this article helpful?

0 0

Post a comment