Java Reference
In-Depth Information
f.setResizable(false);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
Listing 7-6 presents a similar architecture to that shown in Listing 7-2 . However,
it also demonstrates various Swing features, including Swing components/containers,
ImageIcon , tooltips, borders, and setDefaultCloseOperation() .
Because Swing component classes have similar APIs to their AWT counterparts,
you can often just prefix an AWT class name with J to refer to the equivalent Swing
class—don'tforgettochangetheimportstatement.Forexample,prefix Label with J
to change from AWT's Label class to javax.swing.JLabel .
ImageIcon is instantiated to load a thermometer icon image—behind the scenes
MediaTracker isusedtoensurethattheimageiscompletelyloaded.The ImageI-
con instance is then passed to the constructor of each JButton instance so that the
button will display this icon along with its label.
Tooltips are handy for presenting small help messages that assist the user in in-
teracting with the GUI. Listing 7-6 demonstrates this feature by invoking
setToolTipText() on each of the txtDegrees and txtResult textfields.
Whentheusermovesthemouseoveratextfield,atooltipwillappeartorevealitshelp
message.
Listing7-6 attachesanetchedbordertothepanelsurroundingthepairofbuttons,to
setthemapartfromtheothercomponents.Becausethisborderbuttsupagainsttheframe
window,anemptyborderiscreatedandassignedtotheframe'srootpanetoleavesome
space around this window's edges.
The setDefaultCloseOperation() method and its DISPOSE_ON_CLOSE
argument reduces verbosity by disposing of a window (in response to a user close re-
quest) without having to install a window listener.
You may have noticed that I've placed the void fixGUI(Container c)
class method before the pack() method call, instead of placing it after pack() as I
discussed following Listing 7-2 . I previously recommended placing fixGUI() after
pack() because (in AWT) preferred sizes are not available until after the pack()
methodcall,and fixGUI() needstoaccesspreferredsizes.InSwing,preferredsizes
Search WWH ::




Custom Search