Introduction to AWT Controls in Java
Java AWT which is abbreviated as Abstract Window Toolkit is nothing but a set of API’s used in order to develop Graphical User Interface or applications based on windows, AWT controls in java are AWT components that facilitates a user to interact with the graphical user interface in different ways. To use AWT in Java java.awt package is used. This package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List, etc. It contains all the classes for creating the user interfaces and for painting graphics and images.
Each AWT control has been designed to create a GUI interface. For example, TextField is used to create the rectangle shape box which enables users to enter the alphabetic character such as name. TextArea is the same as TextField only difference is its word limit is greater than the TextField, mostly used for address. RadioButton is used used to create a radio button which enables the user to select only one option from the given. CheckBox enables users to select more than one option from the given. The list is used to create the list. The scroll bar enables the user to scroll the page in a vertical and horizontal manner.
Syntax
Here is a syntax of how AWT controls are used:
// importing awt package
import java.awt.*;
// create a class extending Frame component
class extends Frame{
(){
Button button=new Button(""); // create instance of component
button.setBounds(40,90,80,30);// call method to set button position
add(button);// adding component to the container
setSize(400,400);//set size of container
setVisible(true);//set visibility of container to true
}
public static void main(String args[]){
clsobj=new ();
}}
The above syntax shows how to use the Button controls of the AWT package. The above syntax denotes the name of the java class. can be set according to our functionality.
AWT Controls
As we know every user interface contains the following basic things:
1. UI Elements
These cover the basic user interface elements that are visible to the user and through with the user makes interaction with the user interface.
➔ Containers
1. Window
2. Frame
● Dialog
● Panel
➔ Button
➔ Text Fields
➔ Label
➔ Canvas
➔ Choice
➔ Scroll Bar
➔ List
➔ Checkbox
Layout
The function of layout is to decide the alignment of elements on the graphical user interface. Layout Manager is responsible for the orientation of components on the graphical user interface. The following are different layout manager classes available in java.awt package:
- Border Layout: This arranges components to align themselves in five directions namely east, west, north, south, and center of the screen.
- Flow Layout: This aligns components in a directional flow and is the default layout.
- Card Layout: Ever component is treated as a card and only one card can be visible at a time.
- Grid Layout: Components are arranged in a rectangular grid.
Behavior
This defines the action taken when the user interacts with the user interface elements. Different listeners come into the picture in this.
Example of AWT Controls in Java
The following example shows the use of different awtcontrols available in java:
Code:
package com.edubca.awtdemo;
import java.applet.Applet;
// import awt and its subclasses
import java.awt.*;
// class extending applet
public class AWTDemo extends Applet {
// this method gets automatically called
public void init() {
Button button = new Button("This is a button"); // creating a button
this.add(button); // adding button to container
// adding to container
Label labelcources = new Label("Please select your favorite language");
this.add(labelcources);
Choice choice = new Choice(); // creating a choice
choice.addItem("Java");
choice.addItem("C++");
choice.addItem("Python");
choice.addItem("Ruby");
this.add(choice); //adding choice to container
Label label = new Label("Demo Label"); // creating a label
this.add(label); //adding label to container
TextFieldtextfield = new TextField("Demo TextField", 30); // creating a Textfield
this.add(textfield); // adding Textfield to container
}
}
Output:
The above program shows how to use awt controls like buttons, Labels, Choice, and Text Fields in java code.
The above code will produce the following output:
Recommended Articles
This is a guide to AWT Controls in Java. Here we also discuss the introduction and syntax of how awt controls are used along with examples and its code implementation. You may also have a look at the following articles to learn more –