LAB ACTIVITY 1B
AWT COMPONENTS USING FLOW LAYOUT
AWT COMPONENTS USING FLOW LAYOUT
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_1B") and check the box to include the main method.
Click Finish.
Step 2: Setting the Frame Properties
Import AWT Packages: At the top of your class, import the necessary AWT packages.
Class Declaration
public class LabActivity_1B extends Frame:
This declares a public class named LabActivity_1B that extends the Frame class.
Constructor
Constructor Declaration: LabActivity_1B() is the constructor of the class. It is automatically called when an instance of LabActivity_1B is created.
Setting the Frame Title
setTitle("Payroll System");:
The title appears in the title bar of the window, helping users identify the purpose of the window.
Setting the Layout Manager
setLayout(new FlowLayout(FlowLayout.LEFT));:
This line sets the layout manager for the frame to FlowLayout.
FlowLayout: This layout manager arranges components in a left-to-right flow, much like lines of text in a paragraph. If there isn't enough room to fit all components on one line, FlowLayout moves to the next line.
FlowLayout.LEFT: This argument specifies that the components should be aligned to the left side of the container.
Setting the Frame Size
setSize(280, 450);:
This line sets the width and height of the frame.
Width = 280 pixels: The width of the window is set to 280 pixels.
Height = 450 pixels: The height of the window is set to 450 pixels.
Making the Frame Visible
setVisible(true);:
This line makes the frame visible on the screen.
By default, frames are invisible when created. You need to set them visible by calling setVisible(true).
Main Method: The main method creates an instance of LabActivity_1B, which triggers the constructor and and initializes the graphical user interface (GUI)
Step 4: Declare all the components.
Labels: Labels are declared to display static text like "ID", "Name", "Gender", etc.
Label lblTitle, lblid, lblnama, lbljantina, lbldept;
Label lblbasic, lblelaun1, lbljamOt, lblot, lbltotal, lbldeduct;
TextFields: Text fields are declared for user input such as ID, Name, Basic Salary, etc.
TextField txtId, txtNama, txtJantina, txtJabatan;
TextField txtbasic, txtelaun1, txtjamOt, txtot, txttotal, txtdeduct;
CheckboxGroup and Checkboxes: A CheckboxGroup is declared to group radio buttons, allowing the selection of either "Male" or "Female". The checkboxes themselves are declared as well.
Checkbox cbLelaki, cbPerempuan;
CheckboxGroup cbg;
Choice: A Choice component is declared for the department selection dropdown menu.
Choice cjabatan;
Buttons: Buttons are declared for actions such as "Calculate," "Clear," and "Display".
Button btnCalc, btnClear, btnDisplay;
Panels: Panels are declared to group related components together.
Panel panel1, panel2;
TextArea: A TextArea is declared to display multi-line output text.
TextArea txtdisplay = new TextArea(3, 30);
Variables for Calculations: Double variables are declared to hold numerical values used in calculations.
Double basic, allowance, jamot, totalot, totalsalary, deduct;
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.