Java AWT components

Java Abstract Window Toolkit (AWT) is a set of classes and components that enable the creation of graphical user interfaces (GUIs) in Java. AWT provides a wide range of components that can be used to build interactive and user-friendly applications. In this article, we will explore some of the commonly used AWT components.

1. JFrame

JFrame is a top-level container that represents the main window of a Java application. It provides the basic framework for building GUIs and includes features such as title bar, maximize and minimize buttons, and the ability to switch between different application windows.

To create a JFrame, you can use the following code:

import javax.swing.JFrame;

public class MyFrame extends JFrame {
    public MyFrame() {
        setTitle("My JFrame"); // Set the title of the JFrame
        setSize(400, 300); // Set the size of the JFrame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the window on exit
        setResizable(false); // Disable resizing of the JFrame
        setVisible(true); // Make the JFrame visible
    }
    
    public static void main(String[] args) {
        new MyFrame();
    }
}

2. JButton

JButton is a component that represents a clickable button in a GUI. It can be used to trigger events or actions when clicked by the user. To create a JButton, you can use the following code:

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyFrame extends JFrame {
    public MyFrame() {
        setTitle("My JFrame");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        
        JButton button = new JButton("Click Me"); // Create a new JButton with the text "Click Me"
        add(button); // Add the button to the JFrame
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new MyFrame();
    }
}

Conclusion

These are just two of the many components available in Java AWT for building interactive GUIs. The JFrame provides the main window for your application, while the JButton allows you to add clickable buttons to trigger actions.

By using the Java AWT components effectively, you can create user-friendly and visually appealing GUI applications in Java.

References:

#java #awt