0% found this document useful (0 votes)
19 views

Java Applet

Uploaded by

animehv5500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Applet

Uploaded by

animehv5500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Applet

An applet is a small application program that runs within a larger program or system. The term "applet" is
often associated with Java programming language, where it refers specifically to a small application
written in Java that is designed to be transmitted over the internet and executed within a web browser.
Java applets were popular in the early days of the internet for creating interactive web content, such as
animations, games, and interactive forms. They allowed developers to embed dynamic functionality into
web pages without requiring users to download and install separate software.
However, with advancements in web technologies and security concerns, the use of Java applets has
declined in favor of other web development technologies such as HTML5, CSS, and JavaScript. Modern
web browsers have also limited or disabled support for Java applets due to security vulnerabilities
associated with the Java plugin.
Despite their decline in popularity, the concept of applets is still relevant in software development, where
it generally refers to small, specialized applications or components that can be embedded within larger
systems to provide specific functionality. These applets can be written in various programming languages
and serve a wide range of purposes, from graphical user interface components to utility tools within
software applications.

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorldApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello, World!", 20, 20);
}
}

<html>
<head>
<title>HelloWorldApplet</title>
</head>
<body>
<applet code="HelloWorldApplet.class" width="200" height="100">
Your browser does not support Java applets.
</applet>
</body>
</html>

Java AWT
AWT (Abstract Window Toolkit) is a foundational Java package used for creating graphical user
interfaces (GUIs) and handling events in Java applications. It provides a set of components and tools that
allow developers to build interactive and visually appealing applications for various platforms.
Here's an introduction to the key concepts and components of AWT:
1. Components: AWT provides a rich set of GUI components such as buttons, labels, text fields,
checkboxes, radio buttons, menus, and more. These components are used to create the user
interface of Java applications.
2. Layout Managers: AWT includes layout managers that help in arranging GUI components
within containers. Layout managers automatically position and resize components based on
various layout rules. Some commonly used layout managers in AWT are BorderLayout,
FlowLayout, GridLayout, and CardLayout.
3. Event Handling: AWT supports event-driven programming model where actions such as button
clicks, mouse movements, and keyboard input are treated as events. Event handling in AWT
involves registering event listeners or handlers to respond to these events. AWT provides
interfaces such as ActionListener, MouseListener, MouseMotionListener, and KeyListener for
handling different types of events.
4. Graphics: AWT includes classes and methods for drawing graphics and images on the screen.
Developers can use the Graphics class to draw shapes, lines, text, and images onto GUI
components such as JFrame, JPanel, or Canvas.
5. Containers: AWT provides container classes such as Frame, Panel, and Applet to hold and
manage GUI components. Containers are used to organize and group components within a
window or an applet.
6. Platform Independence: One of the key features of AWT is its platform independence. AWT
components are implemented using native platform-specific widgets, which allows Java
applications to have a consistent look and feel across different operating systems.
7. Limitations: While AWT provides basic GUI functionality, it has some limitations compared to
more advanced GUI frameworks like Swing or JavaFX. AWT components are heavyweight,
meaning they rely on the underlying operating system's native GUI toolkit, which can lead to
performance issues and inconsistent behavior across platforms.

Overall, AWT serves as a fundamental building block for developing GUI-based Java applications and
provides developers with the tools needed to create interactive and platform-independent user interfaces.
However, for more advanced and modern GUI development, developers often choose to use Swing or
JavaFX, which offer enhanced features and better performance.

AWT controls
AWT (Abstract Window Toolkit) provides a range of controls, also known as components, that developers
can use to create graphical user interfaces (GUIs) in Java applications. Here's an overview of some
common AWT controls:
1. Button: A Button is a clickable control that triggers an action when pressed by the user. It is
typically used to initiate some form of interaction or process within the application.
2. Label: A Label is a non-editable text component used to display static text or images. Labels are
often used to provide information or captions for other GUI components.
3. TextField: A TextField is an input control that allows users to enter and edit single-line text. It
provides a text editing area where users can input textual data.
4. TextArea: Similar to a TextField, a TextArea allows users to input and edit multi-line text. It
provides a larger editing area suitable for longer text inputs.
5. Checkbox: A Checkbox is a control that represents a binary state, typically used for options that
can be selected or deselected independently. Checkboxes are often used in groups to present
multiple options to the user.
6. RadioButton: RadioButton is a control that allows users to make a single selection from a group
of mutually exclusive options. When one radio button is selected, the others in the group are
automatically deselected.
7. CheckboxGroup: CheckboxGroup is used to group related Checkbox controls together. When
CheckboxGroup is associated with Checkbox controls, only one Checkbox within the group can
be selected at a time.
8. Choice: Choice is a drop-down list control that presents a list of options to the user. Users can
select one option from the list at a time.
9. List: A List is a control that displays a list of items from which users can make multiple
selections. It typically presents a scrollable list of items.
10. Scrollbar: Scrollbar is a control used to navigate content that exceeds the visible area of a
container. It allows users to scroll vertically or horizontally through the content.
11. Panel: A Panel is a container control used to organize and group other GUI components. Panels
are often used to create structured layouts within a window or frame.
These are some of the basic controls provided by AWT for building GUIs in Java applications.
Developers can combine these controls and use layout managers to create complex and interactive user
interfaces tailored to their application's requirements.

import java.awt.*;
import java.awt.event.*;

public class AWTExample {


public static void main(String[] args) {
// Create a Frame (window)
Frame frame = new Frame("AWT Example");

// Create a Button
Button button = new Button("Click Me");

// Add an ActionListener to handle button clicks


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});

// Add the Button to the Frame


frame.add(button);

// Set the size of the Frame


frame.setSize(200, 100);
// Make the Frame visible
frame.setVisible(true);

// Handle closing the Frame


frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

Layout managers
Layout managers in Java's AWT (Abstract Window Toolkit) are responsible for organizing and
positioning GUI components within containers such as Frames, Panels, and Applets. They automate the
placement and sizing of components based on predefined rules, ensuring that the GUI adapts gracefully to
changes in size and content. Here are some commonly used layout managers in AWT:
1. FlowLayout: Components are arranged in a left-to-right flow, wrapping to the next line when the
available space is exceeded. This layout is simple and suitable for arranging components
horizontally in a row.
2. BorderLayout: Components are organized into five regions: North, South, East, West, and Center.
Each region can hold only one component, and the layout manager automatically resizes and
positions components as the container is resized.
3. GridLayout: Components are arranged in a grid of rows and columns. All components have the
same size, and the layout manager evenly distributes them across the available space. GridLayout
is useful when you want to create a uniform grid of components.
4. GridBagLayout: Provides a flexible and powerful layout mechanism. It allows components to be
placed in a grid with different sizes and alignments. GridBagLayout offers precise control over
the positioning and sizing of components but requires more code to configure compared to other
layout managers.
5. CardLayout: Useful for creating multi-pane interfaces where only one panel is visible at a time.
Components are stacked on top of each other like cards, and you can switch between them
dynamically at runtime.
6. BoxLayout: Arranges components in a single row or column, allowing for vertical or horizontal
layouts. It provides more flexibility than FlowLayout for controlling the alignment and sizing of
components along the specified axis.
7. FlowLayout: Components are laid out in a single row, with each component positioned next to the
previous one. When the row is full, subsequent components wrap to the next row.
These layout managers can be used individually or combined to achieve complex GUI designs. Choosing
the appropriate layout manager depends on the desired arrangement of components and the flexibility
required in resizing and positioning them within the container.

You might also like