Umrabulo Session #01 – Basic JFrame
Author: Vuyisile Memani
Module: AOP216D/AOR216D
Date: 13 July 2023
Introduction
In this Mrabulo Session were are going to create a basic frame (form) using the JFrame class. The
JFrame class is located in the javax.swing package. We will also use some of JFrame’s methods to
initialise the created frame. We will conclude this tutorial with a DIY (Do It Yourself) exercise.
Problem statement
Create a frame and display it. The output must resemble the following frame:
Solution
The solution is divided into two parts, A and B. In part A we create the frame, and in B we test it.
1
Part A: Create the frame
Step 1
Launch NetBeans.
Step 2
Create a Java Project. To do that, perform the following tasks:
• Click on File | New Project
2
• Select Java under Categories and Java Class Library under Projects.
• Click Next. Name the project BasicFrame.
3
• Click Finish.
Step 3
Create the frame. To do that, perform the following tasks:
• Right click on the project and select New | Java class
4
• Name the class as MyFirstFrame and package it under za.ac.tut.ui
• Click Finish.
5
• Make the MyFirstFrame class a frame by extending the JFrame.
• Create a constructor for the frame.
6
• Inside constructor set some attributes of the frame and ultimately make it visible.
Step 4
Compile the project. To accomplish the task, do the following:
• Right click on the project and select Clean and Build.
7
• Select the Files tab.
• Expand the folder. Under the dist folder you have a jar file that contains the
MyFirstFrame.class file.
8
Part B: Test the frame in an application
Step 1
Create a Java project application. To do accomplish the task, do the following:
• Click on File | New Project.
• Select Java under Categories and Java Application under Projects.
9
• Click Next. Name the project as BasicFrameApp.
• Click Finish.
10
• Inside the main method, declare a BasicFrame.
• There’s an error. This is because MyFirstFrame is not yet visible inside the application. It is
currently located in the jar file. Let’s include the jar file library to the current project. Perform
the following tasks:
1. Right click on the Libraries folder and select Add Jar/Folder.
11
2. Navigate to the location of the BasicFrame.jar file.
3. Select the file and click on Open.
12
4. Now the BasicFrame library is included in the current project. Now import the class. The
error disappears.
Step 2
Instantiate the frame.
13
Step 3
Run the program. Click on the green icon.
Output:
Conclusion
In this Mrabulo Session we managed to create a frame. We created a frame as a standalone class and
thereafter tested/executed it in a frontend class.
DIY
Create a frame that will have your name and surname as its title.
14
15