Java Reference
In-Depth Information
// Create an instance of the applet to add to the frame.
UIDrawHistApplet applet = new UIDrawHistApplet ();
applet.fInBrowser = false;
applet.init ();
// Create the frame with the title
JFrame f = new JFrame ("Histogram with Gaussian");
f.setDefaultCloseOperation (JFrame.EXIT - ON - CLOSE);
// Add applet to the frame and display the frame.
f.getContentPane ().add (applet);
f.setSize (new Dimension (frame - width,frame - height));
f.setVisible (true);
} // main
} // class UIDrawHistApplet
The conversion of the string obtained from the text field to an integer can throw
an exception. Here we indicate a bad value by showing an error message on the
browser status bar. A more sophisticated approach would open a message dialog
to warn the user of the bad input (see the Web Course Chapter 7: Supplements
section for a discussion and several demonstrations of message dialogs).
When building a GUI you should arrange for the controls to behave in way that
will be natural and intuitive for the user. For example, if the user enters a value
into the text field and does not hit Enter, the value in the text field should still be
used when the “Go” button is selected. This ensures that the value showing in
the text file is always the parameter used in the calculation, as most users would
expect. Furthermore, we added the ActionListener (the applet in this case)
to the list for the JTextfield . Thus, when the user enters text and hits Enter, an
action event will be generated and the actionPerformed() method invoked
just as if the “Go” button had been selected.
The above example also shows how to create a GUI program that runs either
as an applet or as a standalone frame. When run as an applet the main() method
is ignored. When run as an application, the main() method creates an instance
of UIDrawHistApplet and adds it to the frame content pane. The Applet
class getImage() method will not work properly in application mode so we
use a flag to indicate which of the two image loading techniques to use.
7.8 Web Course materials
All of the programs discussed here are available as applets or applications in the
We b Course Chapter 7: Java section along with additional discussions on event
 
Search WWH ::




Custom Search