0% found this document useful (0 votes)
178 views5 pages

Applications in Java.: Java AWT (Abstract Window Toolkit) Is An API To Develop GUI or Window-Based

Java AWT (Abstract Window Toolkit) is an API that provides components to build desktop applications. It contains classes for common GUI elements like buttons, text fields, checkboxes and containers to group these together. There are two main ways to create a basic AWT application: by extending the Frame class or by creating a Frame object directly. For example, a simple app could extend Frame, add a button, set the frame size and layout, and make the frame visible on screen.

Uploaded by

tarun11sep
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)
178 views5 pages

Applications in Java.: Java AWT (Abstract Window Toolkit) Is An API To Develop GUI or Window-Based

Java AWT (Abstract Window Toolkit) is an API that provides components to build desktop applications. It contains classes for common GUI elements like buttons, text fields, checkboxes and containers to group these together. There are two main ways to create a basic AWT application: by extending the Frame class or by creating a Frame object directly. For example, a simple app could extend Frame, add a button, set the frame size and layout, and make the frame visible on screen.

Uploaded by

tarun11sep
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/ 5

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based

applications in java.

1. Java AWT components are displayed according to the view of operating


system.
2. AWT is heavyweight, its components use the resources of OS.
3. The java.awt package provides classes for AWT api such as TextField,
Label, TextArea, RadioButton, CheckBox, Choice, List etc.

The hierarchy of Java AWT classes are given below.


Container
The Container is a component in AWT that can contain another components
like buttons, textfields, labels etc. The classes that extends
Container class are known as container such as Frame, Dialog and
Panel.

Window
The window is the container that have no borders and menu bars. You
must use frame, dialog or another window for creating a window.

Panel
The Panel is the container that doesn't contain title bar and menu
bars. It can have other components like button, textfield etc.

Frame
The Frame is the container that contain title bar and can have menu
bars. It can have other components like button, textfield etc.

Method Description

public void add(Component c) inserts a component on this


component.
public void setSize(int width,int sets the size (width and height) of
height) the component.
public void setLayout(LayoutManager defines the layout manager for the
m) component.
public void setVisible(boolean changes the visibility of the
status) component, by default false.

To create simple awt example, you need a frame. There are two ways to
create a frame in AWT.

a) By extending Frame class (inheritance)


b) By creating the object of Frame class (association)
AWT Example by Inheritance

import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}

AWT Example by Association

import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
First2 f=new First2();
}}
Java AWT Button
The button class is used to create a labelled button that has platform
independent implementation. The application result in some action when the
button is pushed.

AWT Button Class declaration

public class Button extends Component implements Accessible

Java AWT Button Example


import java.awt.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
Button b=new Button("Click Here");
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

Java AWT Button Example with ActionListener


import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f=new Frame("Button Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

You might also like