How to create AWT program in java

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();
    }
}

This Java program creates a simple graphical user interface (GUI) with a button using the AWT (Abstract Window Toolkit) library. Let’s go through it step by step.

1. Importing AWT Package

import java.awt.*;

This imports the AWT package, which contains classes used to create GUI components like Button, Frame, and others. We will use Button and Frame from this package in this program.

2. Class Definition

class First extends Frame {

This declares a class called First that inherits properties of Frame. The Frame class defines a top-level window that includes a title bar, border, and buttons such as minimize, maximize, etc.

3. Constructor Definition

First() {

This is the constructor declaration for the class First. It will be invoked each time an object of the class First is created.

4. Button Declaration

Button b = new Button(“click me”);

This creates a new Button object labeled “click me”. The button is an interactive component that users can click.

5. Setting Button Position

b.setBounds(30, 100, 80, 30);

This line sets the position and size of the button using the setBounds() method. The parameters are:

  • 30: The x-coordinate (horizontal position) of the button on the window.
  • 100: The y-coordinate (vertical position) of the button on the window.
  • 80: The button width.
  • 30: The button height.

6. Add Button to Frame

add(b);

This adds the button b to the frame (the main window) so that it can be displayed .

7. Set Frame Size

setSize(300, 300);

This sets the size of the frame (main window) to 300 pixels wide and 300 pixels high.

8. Disable Layout Manager

setLayout(null);

By default, Frame uses a layout manager (e.g., FlowLayout), which arranges components automatically. Setting the layout to null means that components will be positioned manually, as done for the button using setBounds().

9. Making the Frame Visible

setVisible(true);

This makes the frame visible to the user. Without this, the frame would not appear even if it’s created.

10. Main Method

public static void main(String args[]) {

First f = new First();

}

This is the main method where the program starts execution. It creates an instance of the First class, which triggers the constructor and displays the frame with the button.

11. Program Output

When the program is run, the following will occur:

  • A window (frame) will appear with a size of 300×300 pixels.
  • The button is located at position (30, 100) inside the window with the size 80×30 pixels.
  •  Layout manager is turned off; therefore, button location and size are explicitly managed by using setBounds().

Here is what the window will display:

  • 300×300 window.
  • A button labeled “click me” on the position (30, 100) inside the frame.

Graphical UI Sample Output

Such a window with a specified size and button will open, but it is nothing more than the AWT frame with no other functionality, as button click handling is not implemented yet.

Conclusion:

This is an elementary demonstration of how AWT is to be used in Java. It creates a simple window with a button by manually positioning the button using setBounds() and disabling the default layout manager for the frame. The GUI window with the button is static, but event handling is not defined in this program (what happens when you click the button).

Leave a Comment

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