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:-
- import java.awt.*;
- public class myfrm extends Frame {
- myfrm() {
- Label lab1 = new Label("NORTH");
- Label lab2 = new Label("SOUTH");
- Label lab3 = new Label("EAST");
- Label lab4 = new Label("WEST");
- Button b = new Button("CENTER");
- add(lab1, BorderLayout.NORTH);
- add(lab2, BorderLayout.SOUTH);
- add(lab3, BorderLayout.EAST);
- add(lab4, BorderLayout.WEST);
- add(b, BorderLayout.CENTER);
- }
- public static void main(String arg[]) {
- myfrm frm = new myfrm();
- frm.setSize(700, 400);
- frm.setVisible(true);
- frm.setLocation(100, 10);
- }
- }
Compile
Javac myfrm.java
Java myfrm