Java Reference
In-Depth Information
In this example, the width of the window is set to 275 and the height is set to 100.
By default, when a top-level window is closed (such as when the user clicks the close
box), the window is removed from the screen, but the application is not terminated. While
this default behavior is useful in some situations, it is not what is needed for most applic-
ations. Instead, you will usually want the entire application to terminate when its top-level
window is closed. There are a couple of ways to achieve this. The easiest way is to call
setDefaultCloseOperation( ) , as the program does:
After this call executes, closing the window causes the entire application to terminate. The
general form of setDefaultCloseOperation( ) is shown here:
void setDefaultCloseOperation(int what )
The value passed in what determines what happens when the window is closed. There are
several other options in addition to JFrame.EXIT_ON_CLOSE . They are shown here:
JFrame.DISPOSE_ON_CLOSE
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE
Their names reflect their actions. These constants are declared in WindowConstants ,
which is an interface declared in javax.swing that is implemented by JFrame .
The next line of code creates a JLabel component:
JLabel is the easiest-to-use Swing component because it does not accept user input. It
simply displays information, which can consist of text, an icon, or a combination of the
two. The label created by the program contains only text, which is passed to its constructor.
The next line of code adds the label to the content pane of the frame:
As explained earlier, all top-level containers have a content pane in which components are
stored. Thus, to add a component to a frame, you must add it to the frame's content pane.
This is accomplished by calling add( ) on the JFrame reference ( jfrm in this case). The
Search WWH ::




Custom Search