August 1999
JP219 : JAVA PROGRAMMING

QUESTION 4

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 4

(a) The Java Abstract Windowing Toolkit (AWT) provides the basic Graphical User Interface components which are used in Java applets and applications. Every GUI component that appears on the screen is a subclass of the class Component. There are two major types of components - window and panel.

(i) In the context of the AWT package, describe what is meant by a window.

(ii) In the context of the AWT package, describe what is meant by a panel.

 





[2]


[2]

(i) A window is a
  • free standing native window on the display;
  • which is independent of other containers.

 

(i) An panel
  • identifies an area into which other objects can be placed;
  • and must be placed into a window to be displayed.

 

(b) (i) Identify the purpose of a layout manager with respect to components.

(ii) Although it is possible to turn off the layout manager when positioning or sizing components, this is not usually advisable. State one reason why this is not usually advisable.

(iii) Briefly suggest a better method which may be employed.

[2]

[1]

 

[1]

(i) The layout manager determines the
  • position of a component;
  • and size of a component.

 

(ii)
Turning off the layout manager results in platform-dependent layouts.

 

(iii)
A better approach is to create new subclasses of the layout manager.

 

(c) A Frame is a subclass of a window, with the following properties:
  • They have a title, and resize corners;
  • Inherit from Component, and add components with the add method;
  • Can be used to create invisible Frame objects, with a title specified by String;
  • Have Border Layout as the default layout manager.

(i) Define a class FooFrame, which implements a frame, by extending the frame superclass. Your class should contain a constructor function which creates the frame with a named title bar.

(ii) Define a function main, which is a member of the class FooFrame, which creates an object of type FooFrame with the title "My Frame!" , and using member functions in the Frame superclass, will set the size of the frame to 100 by 100 pixels, and will display the frame.

(iii) Write a function called AddPanel, which is called with an argument fr of type Frame, and will create a Panel, dimensions 20 * 20, and add it to the frame fr.

 

 

 

 

 


[4]

 

[4]

 

[4]

 

(i) A sample definition of FooFrame follows:

public class FooFrame extends Frame {

    public FooFrame(String title) {
        super(title) ;
    }
}

 

(ii) A sample definition of main follows:

public static void main( ) {
    FooFrame fr= new FooFrame(' 'My Frame! ' ') ;
    fr.setSize(100, 100) ;
    fr.setVisible(true) ;
}

 

(iii) A sample definition of AddFrame follows:

public void AddFrame(Frame fr) {
    panel pan = new Panel ( ) ;
    pan.setSize(20, 20) ;
    fr.add(pan) ;
}