Introduction to Frame, Panel, Applet, Button, Layout In Awt


In this article, you will learn what frames, panels, applets, buttons, and layouts are in AWT with examples.

Frame in AWT

In video and animation, frames are individual pictures in a sequence of images.
Some web sites use HTML frames, where the pages are broken up into various areas. Each area consists of an independent web page. Frames allow multiple web pages to all show up in the same page.
  • In graphics and Desktop publishing applications, a rectangular area in which text or graphics can appear.
  • When referring to the internet, a frame is a movable or non-movable portion of a web page that is either loaded from the same server or a different server to help make navigation easier and to bring other content into the page. For example, this technique is often used with internet advertising banners.
  • In communications, a packet of transmitted information.
  • In video and animation, a single image in a sequence of images. See under fps
  • In HTML, refers to dividing the browser display area into separate sections, each of which is really a different Web page. See under frames.
Example: create an empty Frame. Set the title of the frame to "Frame". The frame is initially invisible and must be made visible by invoking its "show()" method.
  1. import java.awt.*;  
  2. public class Example1  
  3. {  
  4.   public static void main(String [] args)  
  5.   {  
  6.     Frame f = new Frame("Frame");  
  8.     f.setSize(300,300);  
  9.   }  
  10. }  


Panel in AWT

To make Applet or Frame layout easier, you break a frame up into regions and compose each of them separately. Each region is called a Panel. If you want something to draw on with drawString and drawLine normally you would use a Canvas. Each can have its own different LayoutManager. Panels don't have any visible bounding lines. You can delimit them with different background colours.
In many types of look and feel, panels are opaque by default. Opaque panels work well as content panes and can help with painting efficiently. You can change a panel's transparency by invoking the setOpaque method. A transparent panel draws no background, so that any components underneath show through. 
Example: A frame with an empty panel
  1. import java.awt.*;  
  2. public class Example1a extends Panel  
  3. {  
  4.  public static void main(String [] args)  
  5.   {  
  6.     Frame f = new Frame("PANNEL");  
  7.     Example1a ex = new Example1a();  
  8.     f.add("Center", ex);  
  9.     f.pack();  
  11.     f.setSize(300,300);  
  12.   }  
  13. }  

Applet in AWT

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application, including the following:
  • An applet is a Java class that extends the java.applet.Applet class.
  • A main() method is not invoked on an applet, and an applet class will not define main().
  • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
  • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
  • Applets are designed to be embedded within an HTML page.
  • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
  • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime
Applets are used to provide interactive features to web applications that cannot be provided by HTML alone. They can capture mouse input and also have controls like buttons or check boxes. In response to the user action, an applet can change the provided graphic content. This makes applets well suited for demonstration, visualization and teaching. There are online applet collections for studying various subjects, from physics to heart physiology. Applets are also used to create online game collections that allow players to compete against live opponents in real-time. 
Example: A frame with an empty applet.
  1. import java.awt.*;  
  2. public class Applet extends java.applet.Applet  
  3. {  
  4.   public static void main(String [] args)  
  5.   {  
  6.      Frame f = new Frame("Applet c");  
  7.      Applet ex = new Applet();  
  8.      f.add("Center", ex);  
  9.      f.pack();  
  11.      f.setSize(300,300);  
  12.    }  
  13. }  

Button in AWT

Buttons are a quick and easy way to allow your users to make choices inside your applets. When the button is pressed an action is generated. 
Example: A Frame with 2 Buttons
  1. import java.awt.*;  
  2. public class Ex3 extends java.applet.Applet  
  3. {  
  4.   public void init()  
  5.    {  
  6.       add(new Button("One"));  
  7.       add(new Button("Two"));  
  8.     }  
  9.   public Dimension preferredSize()  
  10.    {  
  11.       return new Dimension(300,100);  
  12.    }  
  13.   public static void main(String [] args)  
  14.    {  
  15.       Frame f = new Frame("Button");  
  16.       Ex3 ex = new Ex3();  
  17.       ex.init();  
  18.       f.add("Center", ex);  
  19.       f.pack();  
  21.     }  
  22. }  

Layout in AWT

Each container class has a default layout manager. The default layout manager for the Frame class and Dialog class is the BorderLayout manager. The default layout manager for the Panel class (and the Applet class) is the FlowLayout manager. The code in Listing 5 uses both layout managers and includes a few more user interface components.
Example: The result is displayed in the figure. This will generate a frame containing TextArea, Button, ChoiceList.
  1. import java.awt.*;  
  2. public class Ex4 extends java.applet.Applet  
  3. {  
  4.   public void init()  
  5.   {  
  6.     Panel p;  
  7.     setLayout(new BorderLayout());  
  8.     p = new Panel();  
  9.     p.add(new TextArea());  
  10.     add("Center", p);  
  11.     p = new Panel();  
  12.     p.add(new Button("One"));  
  13.     p.add(new Button("Two"));  
  14.     Choice c = new Choice();  
  15.     c.addItem("one");  
  16.     c.addItem("two");  
  17.     c.addItem("three");  
  18.     p.add(c);  
  19.     add("South", p);  
  20.   }  
  21.   public static void main(String [] args)  
  22.   {  
  23.     Frame f = new Frame("Example 4");  
  24.     Ex4 ex = new Ex4();  
  25.     ex.init();  
  26.     f.add("Center", ex);  
  27.     f.pack();  
  29.     f.setSize(300,300);  
  30.   }  
  31. }