import java.awt.*;
public class TextAreaExample
{
TextAreaExample()
{
Frame f = new Frame();
TextArea area = new TextArea("Welcome to TechSarvam");
area.setBounds(10, 30, 300, 300);
f.add(area);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}
}
This Java program shows how a GUI application is created using the AWT library with a TextArea component. A window with a TextArea is constructed for a multi-line text input or display. Now, let us go through the program in small steps.
1. Importing AWT Package
import java.awt.*;
It imports the AWT package that contains classes that are required to create the GUI components, such as Frame, TextArea, and others.
2. Class Definition
public class TextAreaExample
This declares a class called TextAreaExample. The functionality of the program is inside this class.
3. Constructor Declaration
TextAreaExample(){
This is the constructor of the TextAreaExample class. When an object of this class is instantiated, the constructor gets called to initialize the GUI components (in this case, the Frame and TextArea).
4. Frame Creation
Frame f = new Frame();
A Frame object f is declared. A Frame is a top-level window with a title bar, border, and buttons (for example, minimize, close). In this case, the frame is declared without a title. You might also add a title using the new Frame(\”Title\”).
5. Creation of TextArea
TextArea area=new TextArea(\”Welcome to TechSarvam”);
A TextArea object area is constructed with the string “Welcome to TechSarvam” as its default text. It is a multilined textual component that supports the display of or editing long texts.
6. Setting position and size of the TextArea
area.setBounds(10,30,300,300);
The setBounds() method sets the position and size of the TextArea. The parameters are:
- 10: The x-coordinate (horizontal position) of the text area inside the window.
- 30: The y-coordinate (vertical position) of the text area inside the window.
- 300: The width of the text area.
- 300: The height of the text area.
Hence, the TextArea will be at position (10, 30) with a size of 300×300 pixels.
7. Adding TextArea to the Frame
f.add(area);
This line adds the TextArea to the frame (f). This ensures that the text area will be displayed inside the frame.
8. Setting Frame Properties
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
- f.setSize(400, 400);: This is used to set the size of the frame to 400 pixels wide and 400 pixels high.
- f.setLayout(null);: It disables the default layout manager and allows the user to manually position components using setBounds().
- f.setVisible(true);: It makes the frame visible to the user. Without this, the frame would not appear.
9. Main Method
public static void main(String args[]) {
new TextAreaExample();
}
The main method is the starting point of the program. When you run the program, the main method instantiates an object of class TextAreaExample, which as a result calls the constructor and displays the frame with a TextArea inside.
10. Program Output
When you run the program, you will see a window (frame) with the following characteristics:
The window is 400×400 pixels.
- Inside the window, there will be a TextArea that shows the text “Welcome to TechSarvam”.
- The TextArea is at position (10, 30) and has size 300×300 pixels.
Example Output (Graphical UI)
- A window of size 400×400.
- Outside, a window, there will be a TextArea that starts at position (10, 30) and is sized 300×300 pixels and contains the text “Welcome to TechSarvam”.
For this program, since it uses setLayout(null), the size and place of the TextArea are specified using setBounds().
Conclusion:
This program illustrates the following ways of using the TextArea component in Java AWT for creating a very simple GUI with a multiline text field: creating a frame, adding a textarea, and manually placing it inside the frame. As an example, it would be a simple window having a text area displaying an initial message.