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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

344Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProSo what is left for you to do? You have to:• C<strong>on</strong>struct the various items that will appear <strong>on</strong> the screen (in <strong>on</strong>e or morewindows).• Specify the locati<strong>on</strong> of these objects in the windows (layout).• Provide snippets of code that are the acti<strong>on</strong>s associated with various events(events happen, for example, when a butt<strong>on</strong> is pressed or a mouse isclicked). These acti<strong>on</strong>s are the guts of the code that make your applicati<strong>on</strong>behave how you want.Remember, some behaviors are enforced by the GUI as part of standard“look and feel.” Some things, like layout, are up to you to do well so l<strong>on</strong>g asyou keep within the standard UI guidelines. And some is just specific to yourapplicati<strong>on</strong>.With that in mind, let’s walk through the “Hello, world” example for abrief descripti<strong>on</strong> of what each line does.We begin with some import statements, to resolve references to bothSwing and AWT classes. Swing is built <strong>on</strong> top of AWT, so some of the classesthat you use will actually be AWT classes. The biggest difference this makes toyou is in the import statements.We then begin our class declarati<strong>on</strong>, followed by the definiti<strong>on</strong> of the <strong>on</strong>lymethod in this class, main(). Swing applicati<strong>on</strong>s will typically have othermethods, but for our simple example we <strong>on</strong>ly need this <strong>on</strong>e.Now comes some real Swing. The creati<strong>on</strong> of a JFrame object is very important.In Swing we need to have c<strong>on</strong>tainers to hold the objects that we wantto display. The JFrame is a top-level c<strong>on</strong>tainer, <strong>on</strong>e specifically meant to holdother objects, but also meant to be the first <strong>on</strong>e of a c<strong>on</strong>tainment hierarchy.(There are <strong>on</strong>ly three such top-level c<strong>on</strong>tainers in Swing: JFrame, JDialog,and JApplet.)Next we create a JLabel. It’s a Swing object meant to hold small amountsof text. The text can be either c<strong>on</strong>stant or changing, but it’s not user-editable(that would be a different kind of Swing object).We add the label to the frame, so that when the window (the JFrame)appears, it will show our text inside.The setDefaultCloseActi<strong>on</strong>() does what you think. When you pressthe X in the upper right of the window frame (or wherever your window managerputs it), then not <strong>on</strong>ly will the window go away, but the program will stoprunning. (This is not the standard default value since JFrames can c<strong>on</strong>tain

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

Saved successfully!

Ooh no, something went wrong!