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

Introduction

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

1. Frame

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.

Eample: 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.

import java.awt.*;
public class Example1
{
  public static void main(String [] args)
  {
    Frame f = new Frame("Frame");
    f.show();
    f.setSize(300,300);
  }
}

Output

frame.jpg

2. Panel

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 LayoutManagerPanels don't have any visible bounding lines. You can delimit them with differing 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.

Eample: A frame with an empty panel

import java.awt.*;
public class Example1a extends Panel
{
 public static void main(String [] args
)
 
{
    Frame f = new Frame("PANNEL");
    Example1a ex = new Example1a();
    f.add("Center", ex);
    f.pack();
    f.show();
    f.setSize(300,300);
  }
}

Output

pannel.jpg 

3. Applet

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 suitable for demonstration, visualization and teaching. There are online applet collections for studying various subjects, from physics[12] to heart physiology.[3] Applets are also used to create online game collections that allow players to compete against live opponents in real-time.

Eample: A frame with an empty applet.

import java.awt.*;
public class Applet extends java.applet.Applet
{
 
public static void main(String [] args
)
 
{
    
Frame f = new Frame("Applet c");
    
Applet ex = new Applet();
    
f.add("Center", ex);
    
f.pack();
    
f.show();
   
 f.setSize(300,300);
   }
}

Output

applet.jpg 

4. Button

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.

Eample: A Frame with 2 Buttons

import java.awt.*;
public class Ex3 extends java.applet.Applet
{
  public void init
()
  
{
      add(new Button("One"));
      add(new Button("Two"));
    }
  public Dimension preferredSize()
   {
      return new Dimension(300,100);
   }
  public static void main(String [] args)
   {
      Frame f = new Frame("Button");
      Ex3 ex = new Ex3();
      ex.init();
      f.add("Center", ex);
      f.pack();
      f.show();
    }
}

Output

button.jpg 

5. Layout

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.

Eample: The result is displayed in the figure. This will generate a frame containig TextArea, Button, ChoiceList.

import java.awt.*;
public class Ex4 extends java.applet.Applet
{
  public void init
()
  {
    Panel p;
    setLayout(new BorderLayout());
    p = new Panel();
    p.add(new TextArea());
    add("Center", p);
    p = new Panel();
    p.add(new Button("One"));
    p.add(new Button("Two"));
    Choice c = new Choice();
    c.addItem("one");
    c.addItem("two");
    c.addItem("three");
    p.add(c);
    add("South", p);
  }
 
public static void main(String [] args)
  {
   
Frame f = new Frame("Example 4");
    Ex4 ex = new Ex4();
    ex.init();
    f.add("Center", ex);
    f.pack();
    f.show();
    f.setSize(300,300);
  }
}

Output

fig3.jpg