LAB ACTIVITY 2C
SWING COMPONENTS WITH LAYOUTS
SWING COMPONENTS WITH LAYOUTS
Learning Outcomes
By the end of this lab, students should be able to :
Write a Java program using Frame class from Swing package.
Write a Java Program using GUI objects from Swing package.
Hardware/ Software : Personal Computer, Java Development Kit version X.x.x., NetBeans IDE 12.0
LAB ACTIVITY 2C
Question:
Follow the instructions below to build an interface as depicted in the image below. You are required to use Java Swing components to construct this interface and utilize an appropriate layout.
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_2") and click Finish.
Create a New Class:
Right-click on the src folder in your project and select New > Class.
Name your class ( "LabActivity_2C") and check the box to include the main method.
Click Finish.
Step 2: Set Up the JFrame
Import Packages: At the top of your class, import both AWT and Swing packages to access necessary GUI components.
Define the Constructor: Define the constructor for your class LabActivity2C, where you configure the JFrame.
Main Method: The main method creates an instance of LabActivity2C, which activates the constructor and initializes the graphical user interface
Step 3: Set Up the JPanel
To organize components in a Java Swing application using multiple JPanels within a main panel that utilizes a BoxLayout, you need to follow these steps systematically to ensure a structured and orderly layout.
You must using JPanels to organize components as below. We must add all panels Panel1, Panel2, Panel3 and Panel4 inside another 1 panel (Main Panel) with Layout : Box Layout (Click here to read about Box Layout)
Step 5: Set Up JPanel - Panel 1
Create JPanel with name mainPanel and panel1. THen, create Title using Label and add Label inside panel1. And add panel1 inside mainPanel. You can devide your coding based on comments
Here is your output:
Step 6: Set Up JPanel - Panel 2
Create JPanel with name panel2. Then, add all the other components (Label, TextField and RadioButton) inside Panel 2. (Refer Picture in number 3). Set Panel 2 with Grid Layout. Click Here to study about Grid Layout
Here is your Output
Step 7: Set Up JPanel - Panel 3
Create JPanel with name panel3. Then, add all Buttons inside Panel 3. (Refer Picture in number 3). Set Panel 3 with FlowLayout. Click Here to study about Flow Layout
Here is your Output
Step 8: Set Up JPanel - Panel 4
Create JPanel with name panel4. Then, add all Labels inside Panel 4. (Refer Picture in number 3). . Set Panel 4 with Grid Layout. Click Here to study about Grid Layout
Here is your Output
Step 9: Set Up JMenu
Setting up a JMenu in a Java Swing application involves creating a menu bar, adding menus, and incorporating various menu items that users can interact with. This step is crucial for providing a structured way for users to navigate and access different functionalities of an application.
Step 10 :Observe your output.