Professional Documents
Culture Documents
To insert values in an array, we put an equal sign (=) followed by a curly brace
({ }). Inside the curly brace, each value is listed and separated by a comma (,). If
the values are literals, we enclosed them by a double quote (“”). Each individual
value of an array is called an element. For example;
String [] student_names = {“Anna”, “Lorna”, “Fe”};
On the other hand, we can also declare an array of integers. For example:
int [] myNum = {5, 10, 15, 20};
we can also declare an array with no initial values but reserves a memory space
for the elements the array will hold. It is through the way we create an object.
For example:
int [] myNum = new int [4];
1|Page
5 10 15 20
To declare individual values to the array we just declared using this method, we
can simply use the way how we place values on our variables. For example:
int myNum [0] = 5;
int myNum [1] = 10;
int myNum [2] = 15;
int myNum [3] = 20;
CODE:
OUTOUT:
2|Page
A variable that has a primitive type, such as int, holds a value. A
variable with a reference type, such as an array, holds a memory
address where a value is stored. In other words, array names contain
references.
To initialize an array, you use an initialization list of values separated
by commas and enclosed within curly braces. Providing values for all
the elements in an array also is called populating the array.
For example, if you want to create an array named myNum and store
the five numbers within the array, you can declare the array as
follows:
int[] myNum= {5, 10, 15, 20};
You can loop through the array elements with the for loop, and use
the length property to specify how many times the loop should run.
The following example outputs all elements in the myNum array:
3|Page
The programs we develop will be more appreciated by the intended users if they
can interact with it through Graphical User Interface (GUI).
Commonly, GUI are buttons, text fields and the like, that the user can use to tell
that program what to do.
In Java, we use two different GUI components that are already prepared for the
developers to use, namely: the Abstract Windows Toolkit (AWT) and Swing.
The main difference between the two is that, the AWT is not as portable as the
Swing.
A component is a single part of a GUI.
A container is a type of component that holds other components so that we can
treat them as a single entity.
The container is the parent class of the window class.
A window is a container that is rectangular in shape and holds other GUI controls.
JFrame Class is used to as a place to hold other objects to display.
4|Page
To import all the components of a Swing class, we use:
import javax.swing.*;
In setting the size of the JFrame, we call the JFrame identifier followed by the
setSize() method.
The arguments we need to enclosed in the method is the JFrame’s width and
height in pixels (px), for example:
frame1.setSize(250, 100);
For the JFrame to be visible on the screen, we need to set its visibility into true
using the method setVisible(). For example;
frame1.setVisible(true);
The JFrame we just created includes control we typically see on a window. These
are the minimize, restore and close. As developers, we need to ensure that when
we press the close button, the program is terminated. To do this, we can use the
EXIT_ON_CLOSE on the method setDefaultCloseOpertation(). For example;
frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
5|Page
we created the JLabel using the line as follows:
JLabel label1 = new JLabel("Good day!");
The literal strings enclosed in the double quotation is the label or that will appear
inside the frame.
Initially, we cannot see it yet into our frame, unless we use the add() method. For
example:
frame1.add(label1);
We can also set the font of our JLabel by importing the AWT package such as:
import java.awt.*;
and by creating the desired font. For example:
Font font1 = new Font(“Arial”, Font.BOLD, 36);
6|Page
To apply the font on the JLabel, we use the setFont() method. For example:
label1.setFont(font1);
One problem arises when we add multiple components to a JFrame. The only thing
appear on the frame are the last components that were added.
To address this, we use Layout Manager.
A Layout Manager is an object that controls component positioning. It can be a
FlowLayout(), BorderLayout(), BoxLayout(), CardLayout(), GridLayout(),
GroupLayout() or SpringLayout().
7|Page
Java uses the BorderLayout() by default. It divides a container into regions.
However, we can use FlowLayout() so that components do not place on top of each
other. It automatically spills out into the next row when the first row is already
filled.
To set the layout of the JFrame, we use the setLayout() method.
A text field is a component that enables a user to type in a character or series of
characters.
Java swing class enables developers to integrate a text field on a JFrame through
the use of JTextField component.
To create a JTextField, we write the code as follows:
JTextField name_fld = new JTextField(15);
The namefld is the identifier for the JTextField we are creating.
The integer argument inside the parenthesis serves as the size of characters the
JTextField will hold and display.
Buttons are components that is used to trigger an action or make a selection when
the user clicks it.
JButton is a component of swing class that lets the developer create a button.
The line below creates a JButton.
JButton press_btn = new JButton (“Click to Continue”);
Like any other components, we need to add it in the JFrame.
Tooltips are popout windows that help a user understand the purpose of components present
in an application. Tooltips appears when the user hovers the mouse pointer over a
component.
8|Page
9|Page
source - A part on which an event is created within an event-driven
program
actionPerformed() method must have the following header, in which e represents any
name you choose for the Event:
public void actionPerformed(ActionEvent e)
Netbeans is an integrated development environment for Java that allows applications to be
developed from a set of modular components. We use Netbeans IDE to code.
We create a child class by using the keyword extends in the class header, followed by the
parent class name.
implements ActionListener phrase is used to add so that class can respond to ActionEvents.
We can notice that we add the method addActionListener() to the JButton named press.
This is to tell the program that there are actions to be taken when the user clicks the
JButton. 10 | P a g e
11 | P a g e