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.4 Slow, Slow, Quick-Quick, Slow: The Basic Swing Objects345other JFrames, and for most frames you wouldn’t want closing the frame toquit the program.)When we “pack” the frame, that’s when the real magic happens. It kicksoff the GUI activity <strong>on</strong> a separate thread, but also packs the various pieces thatwe’ve added to the frame, sizing them as best it can to fit into the frame, andsizing the frame to hold all the pieces. We see little of that with our example,which has <strong>on</strong>ly <strong>on</strong>e label.As an aside, pack() is inherited from awt.Window, which describes the“magic” thus:A comp<strong>on</strong>ent is displayable when it is c<strong>on</strong>nected to a native screen resource.A comp<strong>on</strong>ent is made displayable either when it is added to a displayablec<strong>on</strong>tainment hierarchy or when its c<strong>on</strong>tainment hierarchy is made displayable.A c<strong>on</strong>tainment hierarchy is made displayable when its ancestorwindow is either packed or made visible.So pack()-ing the frame c<strong>on</strong>nects it to a “native screen resource,” whicheffectively gets the whole GUI thing going.And now back to the business at hand.Finally, the setVisible() call makes the window appear. Then themain() is d<strong>on</strong>e. In case you’re w<strong>on</strong>dering, go ahead and put aSystem.out.println() message after the setVisible(). It will be printedright away. The main() has ended; the GUI activity is happening <strong>on</strong> anotherthread.16.4SLOW, SLOW, QUICK-QUICK, SLOW:THE BASIC SWING OBJECTSLet’s take a quick look at a variety of Swing objects that you can use to build aGUI. Like any job of craftsmanship, it helps to be familiar with your tools.What you can build will be aided (and limited) by the tools with which youbuild, and by how familiar you are with them. Here are some pieces which canbe put together to make Swing GUIs:butt<strong>on</strong>A push butt<strong>on</strong> to trigger acti<strong>on</strong>s; can c<strong>on</strong>tain text and/or an ic<strong>on</strong>.radio butt<strong>on</strong>A group of butt<strong>on</strong>s where <strong>on</strong>ly <strong>on</strong>e value can be chosen at a time.

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

Saved successfully!

Ooh no, something went wrong!