GUI: Applet Tutorial

GUI

GUI stands for Graphical User Interface. We interact with the screen in two ways one is console-based and the second one is a Graphical user interface. In console based we interact with help of text command eg. command prompt whereas in GUI we interact with the help of graphical components which is also known as visual indicators eg. Calculator.

For Graphical User Interface there are two types of Application Programming Interface (API’s) and they are AWT (Abstract Windowing Toolkit) and Swing.

AWT:

Abstract Windowing Toolkit consists of twelve packages. Out of twelve packages, only two packages java.awt and java.awt.event are commonly used.

Java.awt : The java.awt package contains the core AWT, graphic classes.

  • GUI component classes consist of Button, Textfield, and Label.
  • GUI container classes consist of Frame, Panel, Dialog, and ScrollPane.
  • Layout managers consist of FlowLayout, BorderLayout, and Grid Layout.
  • Custom graphics classes consist of Graphics, Color, and Font.

Java.awt.event: The java.awt.event package supports event handling.

  • Event classes consist of ActionEvent, MouseEvent, KeyEvent, and WindowEvent.
  • Event Listener Interface consists of ActionListener, MouseListener, KeyListener, and WindowListener.

What is a Container?

Each GUI program has a top-level container. Frame, Dialog, and applet in Java are the commonly used top-level containers in AWT.

  1. Frame: A Frame is the top-level container of an AWT GUI program. A-frame consists of a title bar (containing an icon, a title and the minimize and the maximize, Close buttons), an optional menu bar, and the content display area.
  2. Panel: A panel is a rectangular area used to group related GUI components. The panel is a sub-container. The panel is used to make sections in the container.

Applet in Java:

An applet in java is a simple java program. Applet runs also in a web browser. Like frame applets are container classes. An applet also extends the java.applet.Applet class. Applets are thus to be embedded within an HTML page. A JVM is necessary to view an applet.

In an applet, we can not make the main( ) function even if it is written in java. It is called a stand-alone application. Compile same as java file.

We can not run as java for that we have two options:

  • Browsers run HTML embed.
  • applet viewer.

Applet’s Life Cycle: 

  • JVM creates an instance of the applet class and invokes various methods during the applet’s lifetime.
  • When we open any HTML file containing an applet class in the browser called JVM and JVM creates an object of our applet class which is a child class of Applet by default that object has five functions from the parent class which is required for the life cycle.
  • Methods in the applet class give you the framework on which you can build any applet application.
Following are the default methods that JVM calls automatically.
  1. init(): run at the start and it is used for initializing purposes. It runs only once.
  2. start():call automatically once the init() function is ended. It may also called one or many times.
  3. stop(): If we navigate to another Webpage then it stops the start() function. it may be called one or many times.
  4. destroy(): It ends the applet lifestyle if we close the Webpage. It runs only once.
  5. paint(): It is not a life cycle part but it is important and called after start().

Components in applet in java:

An applet is a container and components are nothing but the elements present in the applets and they are as follows: Button, TextField, Label, Checkbox, Choice and List, etc. We place components on the applet container so that users can interact with the applet in java. We get all these components from the java.awt package. Let’s take a step by step approach to creating components in an applet through the following example: Where we can add two numbers.

1. The basic structure of an applet:

We run our applet in java through applet viewer. Following a few lines of code is the basic skeleton of an applet in java.

2. Creating component reference variables:

In java create reference variables before creating an object. L1 and l2 are the reference variables of the Label and similarly t1and t2 for Textfeild and b1 for a button. 

3. Initialize components:

As we know when the browser makes an object of the applet then it automatically calls the init() function. so, We override the init() method and initialize components in the init() method.

4. Adding components to the applet:

  • In the step, we add all components that are required.
  • If the setting layout is already set(by default) then the sequence of adding the components is arranged according to the set layout.
  • If we set the setting layout to null then we have to design the setting layout then components are arranged according to our setting layout.

5. Setting layout:

  • There are many setting layouts according to our need if that type of setting is available then pass that setting layout object.
  • We can also do the layout with the help of setLayout() function. 

  b1.setLayout(left,top,width,height);

  b1 from left side, top side, width of b1 and height of b1 in pixels.

Advantages:

  • It also increases the interactivity of users.
  • Easy to use.

Disadvantages:

  • All browsers thus do not support applets.
  • Javascript is more used in browsers.

Conclusion:

Applet In Java provides Graphical User Interface in the web browser. Also with the help of applet, we can achieve functionality on the web so that our website becomes dynamic and user friendly. But applet does not support all the browsers that we use whereas javascript supports all browsers therefore javascript is used more than Applet In Java in the web browser.   

written by: Yash Zadap

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *