by Dinesh Thakur Category: AWT and Applets

In this java Example, we have created a class named MyFrame that extends the Frame class. The constructor of the MyFrame class contains the statements that constructs the user interface.

The statements

FlowLayout layout = new Flowlayout();

setLayout(layout);

sets the layout manager for the container object (i.e. Frame).The layout manager arranges the GUI components in the window i.e. it determines the position and size of all the components in the container. The first statement creates an object of the class Flowlayout. In this layout manager, the components are arranged in the container (i.e. frame) from left to right, then on the next line when there is no more space. The setLayout () method defined in java.awt.Container class used in the second statement sets the new layout manager for the frame. The statements used for creating and setting layout can be combined as follows,

setLayout(new FlowLayout);

The statement,

MyFrame frame = new MyFrame();

in the main () method instantiates an object of MyFrame class which invokes the default constructor. This constructor sets the Flowlayout of the frame and adds two buttons using the add () method with labels OK and Cancel into it.

The statement,

frame.setTitle("Extending Frame Class in Java Example"};

sets the specified argument as the title of the frame. The other statements in the main () method work as usual.

import java.awt.*;

class Myframe extends Frame
{
     Myframe()
     {
        FlowLayout layout = new FlowLayout();
        setLayout(layout);
        Button ok = new Button("OK");
        Button cancel = new Button("Cancel");
        add(ok);   
        add(cancel);
     }
}
class ExtendingFrameClass
{
     public static void main(String args[])
     {
         Myframe frame = new Myframe();
         frame.setTitle("Extending Frame Class in Java Example");
         frame.setSize(350,150);
         frame.setVisible(true);
     }
}

Extending Frame Class in Java Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles