LAB ACTIVITY 1A
AWT COMPONENTS
AWT COMPONENTS
Learning Outcomes
By the end of this lab, students should be able to :
• Use AWT in Java programs
• Create and set the Frame Windows
• Create various AWT components
Hardware/ Software : Personal Computer, Java Development Kit version 1.8.x., JCreator IDE, MS-DOS
Open Your IDE: Start your Integrated Development Environment (IDE) like Eclipse, NetBeans, or IntelliJ IDEA.
Create a New Project:
In your IDE, select File > New > Project.
Choose Java Project and click Next.
Name your project ("LabActivity_1") and click Finish.
Create a New Class:
Right-click on the src folder in your project and select New > Class.
Name your class ( "LabActivity_1A") and check the box to include the main method.
Click Finish.
Import AWT Packages: At the top of your class, import the necessary AWT packages.
Define the Constructor: The constructor LabActivity_1A() is defined but currently empty.
Extend Frame Class: Modify your class definition to extend the Frame class.
Main Method: The main method creates an instance of LabActivity_1A, which triggers the constructor and and initializes the graphical user interface (GUI)
setTitle("Lab Activity 1");:
Purpose: This method sets the title of the window (frame).
Explanation: The setTitle method is used to specify the text that appears in the title bar of the frame. In this case, the title is set to "Lab Activity 1". This title helps identify the window when multiple windows are open.
setLayout(null);:
Purpose: This method sets the layout manager for the frame.
Explanation: The setLayout method is used to define how components will be arranged inside the frame. By passing null, you are specifying that the frame should have no layout manager, which means you can manually position the components using absolute positioning (setting the exact location and size of each component). This is known as a null layout.
setSize(300, 360);:
Purpose: This method sets the dimensions of the frame.
Explanation: The setSize method is used to define the width and height of the frame. In this case, the width is set to 300 pixels, and the height is set to 360 pixels. This determines how large the frame will appear on the screen when the application runs.
setVisible(true);:
Purpose: This method makes the frame visible on the screen.
Explanation: The setVisible method controls whether the frame is displayed or hidden. By passing true, you are instructing the program to show the frame on the screen when the application starts. Without this method call, the frame would not appear, even though it is created.
Labels: Several Label components are declared to display text in the GUI.
TextField: A TextField is declared to allow the user to input text.
CheckboxGroup and Checkboxes: CheckboxGroup is used to group checkboxes, allowing only one checkbox in the group to be selected at a time (like radio buttons). Individual checkboxes are declared for gender selection and course selection.
List: A List component is declared to allow the selection of multiple items from a list of departments.
Choice: A Choice component is declared, which is a dropdown menu for selecting a class.
Buttons: Button components are declared for user actions such as "Submit" and "Clear".
TextArea: A TextArea is declared to display multi-line text output.
Font Setup: Two Font objects (f1 and f2) are created for setting the font style and size for different labels.
Label Initialization: Labels like lblTitle, lblName, and others are initialized with descriptive text. The lblTitle is also customized with a font and color.
TextField Initialization: The TextField txtName is initialized with a width of 20 characters.
Choice Initialization: The Choice component cClass is initialized with several options like "DDT5A", "DDT5B", and "DDT5C".
Checkbox and CheckboxGroup Initialization: The gender selection checkboxes (rbMale, rbFemale) are grouped into cgGender, ensuring that only one gender can be selected at a time. Course selection checkboxes (cbCo1, cbCo2, etc.) are also initialized.
List Initialization: The List component lsDept is initialized with department options like "JTMK", "JKM", "JKE", and "JP".
Button Initialization: The buttons btnSubmit and btnClear are initialized with the text "SUBMIT" and "CLEAR", respectively.
TextArea Initialization: The TextArea txtDisplay is initialized with 5 rows and 30 columns.
Each component is added to the frame using the add() method. For example, add(lblTitle); adds the title label to the frame.
Setting Position and Size: The setBounds(x, y, width, height) method is used to set the position and size of each component within the frame. For instance, lblTitle.setBounds(30, 20, 220, 40); positions the title label at coordinates (30, 20) with a width of 220 pixels and a height of 40 pixels.
Step 8: Run the Application:
Compile and run your LabActivity_1A class.
You should see a window with a label, a text field, and a button.