Thursday, 14 May 2015

Frames in Java

  A Frame implements a resizable window that supports a menu bar, cursor, icon, and title.

Caution
An AWT frame has nothing to do with an HTML frame, which is a concept for defining panes inside a window and was developed by Netscape. A Java frame only applies within an applet window or a Java application. You can't use a Java frame to set up Frames on an HTML page.

This is your basic application-type window, which looks like the windows you'll see in most programs. You can use frames with an applet; in fact, it's the only way to use dialog boxes and menu bars.
Frames generate the same events as windows, which they extend: WINDOW_DESTROY, WINDOW_ICONIFY, WINDOW_DEICONIFY, and WINDOW_MOVED.
The only parameter you can pass to the Frame constructor is a String, which will be the window title. You generally will create your own class that extends Frame and contains event-handling methods that override Component methods such as action, mouseDown, and keyDown. When you extend the class, you can make creator methods with more input parameters. One useful technique when you're using Frames with applets is to pass the applet, using this-the Java construct that refers to the object in whose scope the program line is in-to the Frame so that the Frame methods can invoke applet methods and read/write applet instance variables.
The most useful Frame methods follow.
dispose()
Enables you to free up windowing resources when you're done with a Frame. This is a synchronized method.
int, getCursorType()
Returns the integer constant that defines which cursor currently is displayed. The available Frame final static (constant) values follow:
CROSSHAIR_CURSOR
DEFAULT_CURSOR
E_RESIZE_CURSOR
HAND_CURSOR
MOVE_CURSOR
N_RESIZE_CURSOR
NE_RESIZE_CURSOR
NW_RESIZE_CURSOR
S_RESIZE_CURSOR
SE_RESIZE_CURSOR
SW_RESIZE_CURSOR
TEXT_CURSOR
W_RESIZE_CURSOR
WAIT_CURSOR
Image, getIconImage()
Returns the image being used when the window is reduced to an icon.
MenuBar, getMenuBar()
Returns the frame's menu bar.
String, getTitle()
Returns the frame's title.
boolean, isResizable()
Returns TRUE if the frame can be resized. This attribute can be toggled using the setResizable method.
remove(MenuComponent mb)
Removes the menu bar associated with the frame. This is a synchronized method.
setCursor(int cursor_constant)
Sets the current cursor to the one specified by the input argument.
setIconImage(Image icon)
Sets the icon to be used when the frame is reduced to an icon to the input image.
setMenuBar(MenuBar mb)
Sets the menu bar for the frame. This is a synchronized method.
setResizable(boolean flag)
Changes the frame size if the input parameter is TRUE. If the input is FALSE, the frame is a fixed size.
setTitle(String new_title)
Sets the window title.

The applet in Listing 17.37 shows you how to work with a frame. A button on the applet shows the frame. The user then can enter text in a TextArea on the frame. When the user closes the frame (either with the button in the frame or the one in the applet), the frame is hidden and the text in the frame's TextArea is put into the TextArea in the applet.

Listing 17.37. Working with frames.
import java.awt.*;
import java.applet.Applet;


public class frame_test extends Applet
{
    input_frame a_frame;
    Button b_open, b_close;
    TextArea the_message;
    boolean frame_visible;

    public void init()
    {
        b_open = new Button("Open frame");
        b_close = new Button("Close frame");
        add(b_open);
        add(b_close); 
        the_message = new TextArea(5,20);
        add(the_message); 
        frame_visible = false;
        a_frame = create_dialog("Enter a message");
        // Uncomment these next two lines if you want the frame showing 
        //when the applet starts
        //a_frame.show(); 
        //frame_visible = true;
    }

    public input_frame create_dialog(String the_title) {
        //create a new frame. pass this so that the frame can 
        //access the applet's 
        //instance variables in order to pass information back.
        a_frame = new input_frame(the_title,this);
        //set its size to 200 by 200
        a_frame.resize(200,200); 
        return a_frame; 
    }

    public boolean action(Event the_event, Object the_arg) {
        String item_name; 
        //handle mouse clicks on the two buttons
        if(the_event.target instanceof Button) {
            item_name = (String)the_arg;
            if(item_name.equals("Open frame")) {
                if(!frame_visible) {
                    a_frame.show(); 
                    frame_visible = true;
                } 
            } 
            if(item_name.equals("Close frame")) {
                if(frame_visible) {
                    a_frame.hide(); 
                    frame_visible = false;
                } 
            } 
        }
        return true;
    }


}

class input_frame extends Frame {
    Button close_frame;
    TextArea text_space;
    //this has to be of type frame_test not Applet. If you use Applet 
    //instead of frame_test it
    //will still compile but you won't be able to use my_
    //parent to access the instance
    //variables of the frame_test applet such as frame_visible.
    frame_test my_parent;

    input_frame(String the_title, frame_test host) {
        super(the_title); 
        FlowLayout fl; 

        my_parent = host; 
        //have to define a layout manager for the frame
        fl = new FlowLayout(); 
        setLayout(fl); 
        close_frame = new Button("Close frame");
        add(close_frame); 
        text_space = new TextArea("Enter your input",5,10);
        add(text_space); 
    }
    //By overriding the hide method we ensure that the text
    //from the dialog is sent back
    //to the applet if the frame is closed by clicking on the 
    //close frame button in the
    //applet.
    public synchronized void hide() {
        String the_input; 

        the_input = text_space.getText(); 
        //use the reference to the applet to pass data back by accessing the
        //TextArea component 
        my_parent.the_message.insertText(the_input,0); 
        my_parent.frame_visible = false;
        super.hide(); 
    }

    public boolean action(Event an_event, Object arg) {
        String button_name; 
        String the_input; 

        if (an_event.target instanceof Button) {
            button_name = (String)arg;
            if(button_name.equals("Close frame")) {
                //we don't have to do anything to copy the text to 
                //the applet here because we overrode the 
                //hide method
                hide(); 
            } 
        }
        return true;
    }
}

Notice how the applet extends the Frame class and passes the applet object, using this, to the frame so the Frame methods can work with applet data. In general, creating frames is straightforward because the AWT does most of the work for you. Frames are the basic element for Java application interfaces. 

No comments:

Post a Comment