My First Application with AWT in Java: Start your app development journey with Java
Table of contents
Hello, Devs!
WELCOME back to Mr.Blog421, I'm Hemant Kumar, a computer science student, in this article we are gonna to see how to develop an Application with AWT in Java. So, here we are goanna to learn it with AWT and I'll explain every method which is important in AWT. In the upcoming articles, we'll see some of the projects and apps based on AWT and Swing. So. let's start with AWT-
► What is AWT?
AWT stands for Abstract Window Toolkit.
AWT is an Application Programming Interface (API) used to develop Windows and GUI-based applications.
► Java AWT Hierarchy
I think you're clear with object, so I'm moving for component and container.
► Component
In java AWT Hierarchy, all the elements like Button, TextField, ScrollBars, Label etc. are known as components. We need to add the component to a container to place every component in a order and at a particular position on the screen.
► Container
It's a component that can contain another component. For example, a Frame class is extended by Button class, which means Frame class contains Button class and one more thing, a class that extends the Container class is also known as a Container such as Frame, Dialog, Panel etc.
we are allowed to add a container inside a container so, the container itself is a component (it is clear from the diagram). Here are four types of containers in AWT :
Frame
Panel
Canvas
Dialog
Window
First, we'll see the code and then I'll make you clear with every method. Let's see:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class MyFirstApp extends Frame{
MyFirstApp(){
//addActionListener(this);
addWindowListener(new MyWindowAdapter());
setTitle("MyFirstApp in Java????");
setSize(400,300);
setLayout(null);
setVisible(true);
// setBackground(Color.RED);
}
public void paint(Graphics g){
}
public static void main(String args[]){
new MyFirstApp();
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
As in the above output screenshot, you can see there is a frame of application has been created. As you can see some of the methods I've commented and now first I'll make all the methods clear to you used in this code of 30 lines and, at the last of the article we'll see an app with some colors and with a little beauty.
Understand the code line by line:
So, in the first 4 lines of code, we are importing 4 packages which is completely related to AWT in java. Without these packages, we'll not able to build our app. These are the important in-build packages that must be imported -
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*;
make sure that we're ignoring the comment lines for now, we'll clear it later.
Next, we have a public class named MyFirstApp which extends the Frame class, an in-build class in AWT java. Frame provides the basic window functionality, and contains the constructor method that sets the window properties, such as title, size, layout, and visibility.
MyFirstApp() is a constructor method for the MyFirstApp class. It is called when an object of the class is created using the new keyword in the main method (line no. 20 is calling this constructor). It adds a WindowListener to the window using the addWindowListener() method, which is defined in the Frame class.
The WindowListener is an interface that defines methods to handle window events, such as window opening, window closing, window activation, and so on. In this case, the MyWindowAdapter class is used to handle the windowClosing event, which is triggered when the user clicks the close button on the window.
setTitle() is a method, used to set the title of the application, as you can see in the output screenshot there is a title on the title bar.
setSize() is a method, used to set the size of the application, and how much area will be covered by the application by default.
setLayout() is a method, used to set the layout manager of the container, In this specific code, setLayout(null) is called in the constructor of the MyFirstApp class. This sets the layout manager of the frame to null, which means that no layout manager is used. As a result, any components added to the frame using the add method will be positioned using absolute coordinates, using the setBounds method.
setVisible() is a method, used to make visible our components and applications.
The paint(Graphics g) method is also defined but left empty, which means that this frame will not have any graphics drawn on it.
The main method creates an instance of the MyFirstApp class and runs the application.
One more important thing about MyWindowAdapter and WindowAdapter is that if you will not use these classes, you'll not able to terminate your application, especially when you are not using windowClosing() method inside these classes. Here WindowEvent is a class that is in-build and all the windows related operations have been set already, such as closing your application, minimizing, maximizing and other operations.
Now I think we have enough knowledge. So, we'll personalize the app (a little only):
- First, we'll add a Label in MyFirstApp class:-
public class MyFirstApp extends Frame{
Label L;
This will create a label in your app where you can add text.
- Now, I'm adding an ActionListener*()*
MyFirstApp(){
addActionListener(this);
//other statements
// to use this method you have to implement a class named ActionLister
// here we are not using ActionListener because we are not performing any kind of actions
}
6
To add an action listener to a component, such as a button, you need to first create an instance of a class that implements the ActionListener interface, and then add that instance to the component using the addActionListener method.
- add your label inside your constructor:-
MyFirstApp(){
//addActionListener(this);
addWindowListener(new MyWindowAdapter());
L=new Label("Hello AWT!");
L.setBounds(100,150,120,40);
add(L);
//other statements
}
L=new Label("Hello AWT); is creating an instance of the Label and adding the text "Hello AWT!" to the component.
setBounds() is used to declare the location of the label or other component.
add(L); is adding the instance of the Label in the app.
- set the background color:-
Add a simple method setBackground(); for formatting the background of the application.
MyFirstApp(){
// other statements
setBackground(Color.RED);
//set the red color
}
MyFirstApp(){
// other statements
setBackground(Color.GREEN);
//set the green color
}
Let's see the final code:-
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class MyFirstApp extends Frame{
Label L;
MyFirstApp(){
// addActionListener(this);
addWindowListener(new MyWindowAdapter());
L=new Label("Hello Java!");
L.setBounds(100,150,120,40);
add(L);
setTitle("MyFirstApp in Java????");
setSize(400,300);
setLayout(null);
setVisible(true);
setBackground(Color.RED); //change the Color.RED as Color.GREEN later to see the output2
}
public void paint(Graphics g){
}
public static void main(String args[]){
new MyFirstApp();
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
output 1:
output 2:
Thank You for reading this article. see you soon with a new article.