AWT stands for Abstract Window Toolkit, and it is a set of classes and APIs provided by Java for creating graphical user interfaces (GUIs) in desktop applications.
AWT provides a wide range of components for creating GUIs, such as buttons, labels, text fields, menus, and more. These components can be arranged and combined to create complex user interfaces.
Here is an example of how to create a simple window using AWT:
import java.awt.*; import java.awt.event.*; public class MyWindow extends Frame { public MyWindow() { super("My Window"); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new MyWindow(); } }
In this example, we import the necessary AWT packages and define a class called MyWindow that extends the Frame class. We then create a constructor for MyWindow that sets the title of the window, sets its size to 300x200 pixels, and makes it visible. Finally, we create a main method that creates a new instance of MyWindow.
Here is an example of how to create a button and handle its events using AWT:
import java.awt.*; import java.awt.event.*; public class MyButton extends Frame implements ActionListener { private Button button; public MyButton() { super("My Button"); button = new Button("Click me!"); button.addActionListener(this); add(button, BorderLayout.CENTER); setSize(300, 200); setVisible(true); } public void actionPerformed(ActionEvent e) { button.setLabel("Clicked!"); } public static void main(String[] args) { new MyButton(); } }
In this example, we create a new class called MyButton that extends Frame and implements the ActionListener interface. We create a Button object with the label "Click me!" and add an action listener to it (which is the MyButton object itself). We then add the button to the center of the frame using the BorderLayout layout manager. Finally, we define an actionPerformed method that changes the label of the button to "Clicked!" when it is clicked. We create a main method that creates a new instance of MyButton.