Introduction to Frame Using NetBeans IDE in Java

In this article, we are going to develop an application using Frame and learn about Frame class briefly.

Introduction

 
This article shows how to add several features on a frame using Java. The example in this article opens multiple frames from a single frame using menu items. Let's proceed to explore the Frame features of Java.
 

Frame in Java

 
First of all one should know what exactly a Frame is. Frame is a class of the java.awt package. A Frame is a resizable and movable window with a title bar and maximize, minimize and close buttons. A Frame is a container that can have MenuBars, MenuItems and some other components like label, button, textfield, radio button and so on. A Frame is the most important component of the Abstract Window Toolkit (AWT) that is treated as a complete window for specific software.
 

How to Create a Frame in Java

 
A Frame can be created in one of two ways. By adapting any of the following two ways, we will be able to get a Frame. The two ways are:
  1. By following the simple concept of inheritance i.e., by extending the Frame class. We will be considering this method in the article for demonstration purposes and will get a brief idea of it.
  2. By creating an object of the Frame class, in other words using association.
If the second way is adapted then the following concepts are to be considered.
 
Constructor of Frame class
  • Frame(): It constructs a new Frame with no title that was initially invisible.

    public Frame()
  • Frame(String title): It constructs a Frame with some title as specified by the programmer.

    public Frame(String title)
  • Frame(String title,image icon): It constructs a frame with a title as specified by the programmer as well as an image icon, as shown in following screenshot.

    fig6.5.jpg

    public Frame(String title,image icon)
Methods of the Frame class
  • setTitle(String value): This method provides a title to the Frame if not specified at the time of object declaration of the Frame.

    Frame object.setTitle("String value");
  • String getTitle(): This method returns the title of the Frame.

    String variable=Frame object.getTitle();
  • setSize(int row,int col): This method sets the size of the Frame, row and column wise.

    void setSize(int row,int col);
  • setVisible(boolean mode): It makes the Frame visible to us. If the parameter ed is "true" then it will be visible otherwise if it is "False" then the Frame will not be visible.

    void setVisible(boolean mode);
  • setState(int state): It sets the state of the Frame as:

    "0" for Normal state
    "1" for iconified state i.e., minimized
    "2" for deiconified state i.e., maximized

    void setState(int state);
  • setResizable(boolean mode): It sets the frame in the position that it can be resized or can be fixed.

    "true"- resizable
    "False"- can not be resized.

    public setResizable(boolean mode);
  • setBackground(Color.color name): It sets some specified color on the background of the Frame.

    Frame object.setBackground(Color.color name);
Frame Application
 
In this application, there is a Frame whose menu retrieves two other Frames with different Fields inside it. These Frames contain different Labels and TextFields to collect the individual's information. The source code is as follows:
 
Source Code
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. class prg1 extends Frame implements ActionListener  
  4. {  
  5.     MenuBar mb;  
  6.     MenuItem mi1, mi2;  
  7.     Menu m1;  
  8.     Label l1, l2;  
  9.     public prg1()  
  10.     {  
  11.         setLayout(new FlowLayout());  
  12.         mb = new MenuBar();  
  13.         m1 = new Menu("Frame");  
  14.         mb.add(m1);  
  15.         mi1 = new MenuItem("Frame1");  
  16.         m1.add(mi1);  
  17.         mi2 = new MenuItem("Frame2");  
  18.         m1.add(mi2);  
  19.         l1 = new Label("To Fill personal perticulars go to Frame1");  
  20.         l2 = new Label("To fill general perticulars go to Frame2");  
  21.         add(l1);  
  22.         add(l2);  
  23.         mi1.addActionListener(this);  
  24.         mi2.addActionListener(this);  
  25.         setMenuBar(mb);  
  26.     }  
  27.     public void actionPerformed(ActionEvent e)  
  28.     {  
  29.         if (e.getSource() == mi1)  
  30.         {  
  31.             prg2 p = new prg2();  
  32.             p.setSize(300300);  
  33.             p.setVisible(true);  
  34.         }  
  35.         if (e.getSource() == mi2)  
  36.         {  
  37.             prg3 p1 = new prg3();  
  38.             p1.setSize(300300);  
  39.             p1.setVisible(true);  
  40.         }  
  41.     }  
  42. }  
  43. class prg2 extends Frame implements WindowListener  
  44. {  
  45.     Label l, b1, b2, b3, b4, b5;  
  46.     TextField t1, t2, t3, t4, t5;  
  47.     public prg2()  
  48.     {  
  49.         setLayout(new FlowLayout());  
  50.         l = new Label("this is frame 1");  
  51.         b1 = new Label("First Name");  
  52.         b2 = new Label("Last Name");  
  53.         b3 = new Label("Father's Name");  
  54.         b4 = new Label("Date of Birth");  
  55.         b5 = new Label("Qualification");  
  56.         t1 = new TextField(20);  
  57.         t2 = new TextField(20);  
  58.         t3 = new TextField(20);  
  59.         t4 = new TextField(20);  
  60.         t5 = new TextField(20);  
  61.         add(l);  
  62.         add(b1);  
  63.         add(t1);  
  64.         add(b2);  
  65.         add(t2);  
  66.         add(b3);  
  67.         add(t3);  
  68.         add(b4);  
  69.         add(t4);  
  70.         add(b5);  
  71.         add(t5);  
  72.         addWindowListener(this);  
  73.     }  
  74.     public void windowActivated(WindowEvent e)  
  75.     {  
  76.     }  
  77.     public void windowDeactivated(WindowEvent e)  
  78.     {  
  79.     }  
  80.     public void windowOpened(WindowEvent e)  
  81.     {  
  82.     }  
  83.     public void windowClosed(WindowEvent e)  
  84.     {  
  85.     }  
  86.     public void windowClosing(WindowEvent e)  
  87.     {  
  88.         setVisible(false);  
  89.     }  
  90.     public void windowIconified(WindowEvent e)  
  91.     {  
  92.     }  
  93.     public void windowDeiconified(WindowEvent e)  
  94.     {  
  95.     }  
  96. }  
  97. class prg3 extends Frame implements WindowListener  
  98. {  
  99.     Label l, b1, b2, b3, b4;  
  100.     TextField t1, t2, t3, t4;  
  101.     public prg3()  
  102.     {  
  103.         setLayout(new FlowLayout());  
  104.         l = new Label("this is frame 2");  
  105.         b1 = new Label("Sports");  
  106.         b2 = new Label("Hobbies");  
  107.         b3 = new Label("Arts");  
  108.         b4 = new Label("Other Intrests");  
  109.         t1 = new TextField(20);  
  110.         t2 = new TextField(20);  
  111.         t3 = new TextField(20);  
  112.         t4 = new TextField(20);  
  113.         add(l);  
  114.         add(b1);  
  115.         add(t1);  
  116.         add(b2);  
  117.         add(t2);  
  118.         add(b3);  
  119.         add(t3);  
  120.         add(b4);  
  121.         add(t4);  
  122.         addWindowListener(this);  
  123.     }  
  124.     public void windowActivated(WindowEvent e)  
  125.     {  
  126.     }  
  127.     public void windowDeactivated(WindowEvent e)  
  128.     {  
  129.     }  
  130.     public void windowOpened(WindowEvent e)  
  131.     {  
  132.     }  
  133.     public void windowClosed(WindowEvent e)  
  134.     {  
  135.     }  
  136.     public void windowClosing(WindowEvent e)  
  137.     {  
  138.         setVisible(false);  
  139.     }  
  140.     public void windowIconified(WindowEvent e)  
  141.     {  
  142.     }  
  143.     public void windowDeiconified(WindowEvent e)  
  144.     {  
  145.     }  
  146. }  
  147. class callFrm  
  148. {  
  149.     public static void main(String arg[])  
  150.     {  
  151.         prg1 p = new prg1();  
  152.         p.setSize(300300);  
  153.         p.setVisible(true);  
  154.     }  
  155. }  
Run the Code
 
fig6.1.jpg 
 
Output
 
fig6.2.jpg
 
fig6.6.jpg
 
If frame1 is selected then the following frame will appear:
 
fig6.3.jpg
 
If Frame2 is selected then the following Frame will appear:
 
fig6.4.jpg