Java Reference
In-Depth Information
figure B.13
CloseableFrame
class
using
WindowAdapter
and inner class
1
// Frame that closes on a window-close event: (works!)
2
public class CloseableFrame extends JFrame
3
{
4
public CloseableFrame( )
5
{ addWindowListener( new ExitOnClose( ) ); }
6
7
private class ExitOnClose extends WindowAdapter
8
{
9
public void windowClosing( WindowEvent event )
10
{ System.exit( 0 ); }
11
}
12
}
figure B.14
CloseableFrame
class
using
WindowAdapter
and anonymous inner
class
1
// Frame that closes on a window-close event: (works!)
2
public class CloseableFrame extends JFrame
3
{
4
public CloseableFrame( )
5
{
6
addWindowListener( new WindowAdapter( )
7
{
8
public void windowClosing( WindowEvent event )
9
{ System.exit( 0 ); }
10
}
11
);
12
}
13
}
is horrific, but experienced readers of Java code skip over those syntactic
details and easily see what the event-handling code does. The main benefit
here is that if there are lots of small event-handling methods, they need not be
scattered in top-level classes but instead can be placed near the objects that
these events are coming from.
Here is a summary of how to create a GUI application. Place the GUI func-
tionality in a class that extends
JPanel
. For that class, do the following:
1.
Decide on the basic input elements and text output elements. If the
same elements are used twice, make an extra class to store the com-
mon functionality and apply these principles on that class.
Search WWH ::
Custom Search