Java Reference
In-Depth Information
if
(numStr1.equals(""))
return
"E R R O R: Invalid First Number" ;
else
return
"E R R O R: Invalid Second Number" ;
}
catch
(Exception e)
{
System.out.print(beep);
return
"E R R O R";
}
}
Before writing the complete program, we must do one more thing. When the user
clicks the window closing button, the program must terminate. Clicking the window
closing button generates a window event. Therefore, we must create a
WindowListener
object and register the object of the class containing the applica-
tion program because this class extends the definition of the
class
JFrame
. The
window events are handled by the
interface
WindowListener
. To terminate the
program when the user clicks the window closing button, we must provide the
definition of the method
windowClosing
of the
interface
WindowListener
.
Because the
interface
WindowListener
contains more than one method and
we only want to use the method
windowClosing
, we use the mechanism of the
anonymous class to create and register the window event object. To do so, we make
the class containing the application program use the
class
WindowAdapter
to create
and register the window event object. Creating and registering the window event
object is accomplished by the following statements:
this
.addWindowListener(
new
WindowAdapter()
{
public void
windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
We can now outline the program listing.
PROGRAM LISTING
//************************************************************
// Author: D.S. Malik
//
// GUI Calculator Program
// This program implements the arithmetic operations.
//************************************************************
Search WWH ::
Custom Search