Create frame in java

Introduction 

 
In this blog, we will know how to create a frame in java.
 
This is a java application, which provides the graphical user interface to accept users, and provides output in a graphical way. This cannot execute in the web browser. The program's execution starts from main method as a stand-alone application.
 

The creation process of frame

 
1. Create a class, which must inherit java.awt.frame class.
2. Create the constructor to initialize different components.
3. Change the layout of the frame if required from the border layout to the required layout (flow layout, grid layout, grid bag layout, card layout).
4. Provide the paint () method to draw different shapes into the frame.
5. Initialize the class and provide size and visibility property.
 

Difference between frame and applet

 

Frame: -

 
1. This is a GUI for a stand-alone application.
2. This cannot execute in the web browser.
3. Execution starts from main () method.
4. This can access local system resources.

Applet: -

 
1. This is a GUI for web applications.
2. This can execute in web browsers.
3. Execution starts from init (), start () or paint () method
4. This can access resources like (files, Dbms) of the system where it executes.
 
Example:-
  1. import java.awt.*;  
  2. public class myfrm extends Frame {  
  3.  myfrm() {  
  4.   Label lab1 = new Label("NORTH");  
  5.   Label lab2 = new Label("SOUTH");  
  6.   Label lab3 = new Label("EAST");  
  7.   Label lab4 = new Label("WEST");  
  8.   Button b = new Button("CENTER");  
  9.   add(lab1, BorderLayout.NORTH);  
  10.   add(lab2, BorderLayout.SOUTH);  
  11.   add(lab3, BorderLayout.EAST);  
  12.   add(lab4, BorderLayout.WEST);  
  13.   add(b, BorderLayout.CENTER);  
  14.  }  
  15.  public static void main(String arg[]) {  
  16.   myfrm frm = new myfrm();  
  17.   frm.setSize(700400);  
  18.   frm.setVisible(true);  
  19.   frm.setLocation(10010);  
  20.  }  
  21. }  
Compile
 
Javac myfrm.java
Java myfrm