You are on page 1of 11

Arrays are used to store different variables of the same data type in one

place. To declare an array, define a variable type with the square


brackets. For example: String [] student_names;

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];

The new keyword instantiates a new array.


A subscript is an integer contained within square brackets that specifies
one of an array’s elements. In Java, any array’s elements are numbered
beginning with 0, so you can legally use any subscript from 0 through 3 when
working with an array that has 4 elements.
In other words, the first myNum array element is myNum[0] and the last
myNum element is myNum[3].
If you use a subscript that is too small (that is, negative) or too large for an
array, the subscript is out of bounds and an error message is generated.

1|Page
5 10 15 20

myNum[0] myNum[1] myNum[2] myNum[3]

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 creating a JFrame, we need to instantiate it as a new object such as:


JFrame frame1 = new JFrame(“Frame 1”);
The literal String we enclosed on the double quotation mark will be the title that
will be printed on the window.

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);

A JLabel is another type of component that appears in the JFrame as edit.


text we cannot Take a look on the sample code and output below;

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.

We just need to use a method setToolTipText() to enable tooltips in a component.

8|Page
9|Page
source - A part on which an event is created within an event-driven
program

A listener is an entity that is interested in an event.


If we desire to put actions on our components since our program’s intention is to
performance specified task, we make use of listeners.
A source and a listener can be the same object.
The button is the source of the event and the label is a listener. When the source fires an
event, an event- handling method contained in the listener object’s class responds to the
event.
The Event Handler is the classes and methods that take care of the events caused.
One example of event handlers is the actionPerformed() method.

 actionPerformed() methods must be written to overload the empty version in the


interface.

 actionPerformed() method executes automatically.

 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

The this keyword stands as the reference of the action.


The @Override allows a subclass or child class to provide a specific implementation of a
method that is already provided by one of its super-classes or parent classes. In this
example, the block of codes implements method from ActionListener().
Inheritance is a mechanism that allows one class to absorb all of another class's
characteristics and attributes, so you can build a new class simply by specifying how it
varies from a class that has already been Illustrated and checked.
Subclass The class which inherits the properties of other.
superclass (base class, parent class) (derived class, child class) and the class whose
properties are inherited
extends is the keyword used to inherit the properties of a class.
The super keyword is similar to this keyword
Following are the scenarios where the super keyword is used.
 It is used to differentiate the members of superclass from
the members of subclass, if they have same names.
 It is used to invoke the superclass constructor from subclass.
the implements keyword is used with classes to inherit the properties of an interface.
Interface can never be extended by a class.

11 | P a g e

You might also like