13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

352Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProGUI pieces—the status line, the table list of accounts, and the butt<strong>on</strong>s. TheJFrame is a bit odd here, in that you have to add objects to its c<strong>on</strong>tent pane;other c<strong>on</strong>tainer objects you can just add to directly. (We could have d<strong>on</strong>e thegetC<strong>on</strong>tentPane() <strong>on</strong>ce, store the result in an intermediate variable, and dothe adds to it, but the efficiency gain is unimportant here because we <strong>on</strong>ly needto do this <strong>on</strong>ce, to get the GUI started.)When we’ve got it built, we pack the frame, and make it visible:frame.pack();frame.setVisible(true);That’s the basic core of what you need to do with any GUI: c<strong>on</strong>struct itspieces, add them to the frame, pack the frame, and make it visible. Now you’reoff and running. The rest is just details.16.7.2 Creating PiecesThe three pieces that we create—the status, the list, and the butt<strong>on</strong>s—will eachpackage up their objects into an intermediate c<strong>on</strong>tainer, a JPanel, and returnthat c<strong>on</strong>tainer to main(). This not <strong>on</strong>ly serves to chunk the problem intofewer pieces (just three parts, not eight or more), but also helps with theformatting. Each piece can format its objects relative to each other. Thenmain() <strong>on</strong>ly has to lay out the three big pieces. So watch for each of thecreate...() methods to return a JPanel—a good approach when you buildyour GUIs, too.The JPanels returned to main() are just Swing objects. They, like thebutt<strong>on</strong>s or labels (that we will see here shortly), just get added into other c<strong>on</strong>tainers.For main(), that c<strong>on</strong>tainer is the JFrame, the main window. Anyc<strong>on</strong>tainer will have a layout manager, the mechanism by which objects areplaced in that c<strong>on</strong>tainer. For JFrame, the default is the BorderLayout manager.When you call the add() method <strong>on</strong> a c<strong>on</strong>tainer using a BorderLayout,you can specify (as a sec<strong>on</strong>d parameter to the add() method) where theobject being added will get placed. The c<strong>on</strong>stants defined for placingobjects are NORTH, SOUTH, EAST, WEST, or CENTER—hence the “Border” ofBorderLayout. There are also relative positi<strong>on</strong> values: PAGE_START,PAGE_END, LINE_START, and LINE_END which are just like north, south, west,and east, respectively, provided that the Comp<strong>on</strong>entOrientati<strong>on</strong> is set toLEFT_TO_RIGHT. (If you really want to know, check the Javadoc page forjava.awt.BorderLayout.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!