Creating the main window The application must create a main window within which all its output will be confined. It is usual to set the initial size of the window, but the user may change it. Normally the main window will be an object of a subclass of Frame. Why not just create a Frame object? The program can create a main window simply by creating an object of type Frame . For example, the following two line will make a window appear: MainWindow mainWindow = new Frame("My programs title"); mainWindow.setVisible (true);
The problem with this is that the built in Frame class cannot do anything specific. It provides a lot of standard functionality, but it wont do anything useful for a given application. We create a subclass of Frame to do those things that are specific to this application. In particular, objects of class Frame will not stop the program when the user clicks on the close button in the corner of the window, which is the usual convention. So we would normally define a main window as follows: class MainWindow extends Frame { // MainWindow methods go here public MainWindow() // constructor { super("My programs title"); }
With this definition inside our main() method for the application we would create the main window by executing: MainWindow mainWindow = new MainWindow(); mainWindow.setVisible (true);
Although the class MainWindow does not yet have any functional methods, it must have a constructor. The constructor in this case simply calls the constructor for Frame, passing the name of the program as the argument. This name will appear in the caption bar of the window. It also sets the initial size of the window using setSize() . Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |