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.

16.7 Getting Down to Cases: Designing a GUI for BudgetPro355object needs to be told about (i.e., given a reference to) the c<strong>on</strong>tainer (here,retval, a JPanel) whose objects it will manage, but that the c<strong>on</strong>tainer als<strong>on</strong>eeds to be told (via setLayout()) about the BoxLayout object. A bitc<strong>on</strong>fusing, perhaps.Another handy part of BoxLayout is the uses of rigid areas, invisible objectsthat do nothing except putting some space between objects. These rigidareas are defined in pixels; for our GUI we create them with no height and awidth of ten pixels. They are held together using “horiz<strong>on</strong>tal glue” (see line 226)226 retval.add(Box.createHoriz<strong>on</strong>talGlue());so that if the window is stretched, the extra space doesn’t get added betweenthe butt<strong>on</strong>s, but <strong>on</strong>ly to the “glue” comp<strong>on</strong>ent, which absorbs all extra space.This keeps all the butt<strong>on</strong>s to the right hand side of the window.16.7.2.4 JButt<strong>on</strong>sThe method named createButt<strong>on</strong>s() actually packs up the butt<strong>on</strong>s into aJPanel to return to the caller. It begins like this:218 private Comp<strong>on</strong>ent219 createButt<strong>on</strong>s(JRootPane root)220 {221 JPanel retval = new JPanel(); // default: flow layout222223 //Lay out the butt<strong>on</strong>s from left to right.224 retval.setLayout(new BoxLayout(retval, BoxLayout.X_AXIS));225 retval.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));226 retval.add(Box.createHoriz<strong>on</strong>talGlue());227 retval.add(creat);228 retval.add(Box.createRigidArea(new Dimensi<strong>on</strong>(10, 0)));229 retval.add(view);230 retval.add(Box.createRigidArea(new Dimensi<strong>on</strong>(10, 0)));231 retval.add(clos);The butt<strong>on</strong>s themselves were created at the beginning of this class, inlines 27–29, thus:27 private JButt<strong>on</strong> creat = new JButt<strong>on</strong>("New Subaccount");28 private JButt<strong>on</strong> view = new JButt<strong>on</strong>("View Subaccount");29 private JButt<strong>on</strong> clos = new JButt<strong>on</strong>("Quit");

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

Saved successfully!

Ooh no, something went wrong!