140 likes | 148 Views
Learn about different layout managers in Java, including BorderLayout, FlowLayout, BoxLayout, and GridLayout. Explore data entry components such as CheckBoxDemo, RadioButtonDemo, ComboBoxDemo, and TextFieldDemo.
E N D
Layout, Components, & Data Entry Appendix C.3-C.5
Layout Managers • BorderLayout • Arranges objects in five areas of the container. • FlowLayout • Arranges objects in left-to-right order. • BoxLayout • Arranges objects in a single row or column. • GridLayout • Arranges objects in a two-dimensional grid.
BorderLayout • Default layout for JFrame’s content pane. • 5 areas (N,S,E,W,Center) • Don’t have to fill out all areas. • See Listing and Example. • Be sure to use Java reference. • Java Reference Document • Remember to look up class hierarchy. • Use Index. • Find setBorder? • Constants.
FlowLayout • Left to Right until maximum width. • Dimension for array size. • PreferredSize (20, 20). • Show resize of window. • Last line centered if not full. • Use setAlignment to change order. • See Listing and Example.
BoxLayout • Horizontal or Vertical arrangement. • BoxLayout.X_AXIS • BozLayout.Y_AXIS • Components don’t wrap. • See Listing and Example. • What’s missing from this code? • (Hint: related to buttons.)
GridLayout • Rectangular grid. • Fixed Size (set by setLayout(new GridLayout( 5,10) ) • 5 rows and 10 columns. • See Listing and Example. • Note Integer.toString (conversion method) • Factories described on page 183.
Combining Layouts • Lab #3 (Programming #2, page 785)
Components for Data Entry • So what does a good GUI do? • Enable the user to provide input data to an application. • Enable the user to control the action of the application. • Display information or results to the user. • Are there any “bad” Guis? • Web Pages That Suck Poor Design • Interface Hall Of Shame Shame
CheckBoxDemo • Multiple selections possible. • Check mark shows selection. • See Listing and Example. • Note listeners.
RadioButtonDemo • Only one selection possible. • Old time “radio buttons”, press one and it resets the others. • See Listing and Example. • Note selections.length used to control number of buttons. • Also, note the loop to define buttons.
ComboBoxDemo • Selections shown by text. • Drop-down list. • See Listing and Example • Note, no loop.
TextFieldDemo • Label and field to type in. • See Listing and Example. • Note NumberEntered uses an exception.
Example Data Entry in a GUI • VolumeConverterGUI • Fairly complicated constructor. • Enter in one field, output in the other. • Notice how the gui is built-up in layers. • JFrame’s content pane set to a 2x2 grid. • The grid contains labels and text boxes. • Two files to look at. • See Listing and Example.
CurrencyDemo • An example of querrying “where am I”? • Using the Java locale.