Using and Working with Frame in Java Explanation with Example


Working with Frame Windows  


  • Frame class is used to create child windows within applets, and top-level or child windows for stand-alone applications. 
  • it creates a standard-style window.
  • Here are two of Frame’s constructors:
  • Frame( )
  • Frame(String title)
  • The first form creates a standard window that does not contain a title. 
  • The second form creates a window with the title specified by title. 
  • Notice that you cannot specify the dimensions of the window. 
  • Instead, you must set the size of the window after it has been created.


Using Frame
  
  • There are several key methods you will use when working with Frame windows. 
  • The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
  • void setSize(int newWidth, int newHeight)
  • void setSize(Dimension newSize)
  • The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize.
  • The dimensions are specified in terms of pixels.
  • The getSize( ) method is used to obtain the current size of a window. Its signature is shown here:
  • Dimension getSize( )
  • This method returns the current size of the window contained within the width and height fields of a Dimension object.
Example of Using a Frame in Java Programming Language

Example1 

import java.awt.*;
class MyFrame 
{
public static void main(String as[])
{
Frame f=new Frame("My Window");
f.setSize(200,200);
f.setVisible(true);
}
}




Post a Comment

0 Comments