Professional Documents
Culture Documents
Module No. and Title Module 4: Package and Application Programming Interface
Development and Graphical User Interface
Activity Week 1:
Let us start the lesson with an activity. At the end of the activity,
you will be able to make a program that is fully functioning. We
will call this program Swing Counter. Start by following the step-
by-step instructions.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public SwingCounter() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Counter"));
tfCount = new JTextField("0");
tfCount.setEditable(false);
cp.add(tfCount);
btnCount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
++count;
tfCount.setText(count + "");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Swing Counter");
setSize(300, 100);
setVisible(true);
}
Step 2: Save, compile, and run the code. You will get a window
with a label “Counter”, a textfield with “0” as the displayed
number, and a button with label “Count”.
Project WRITE XI: An Easy Guide for Course Pack making and Module Development
Figure 4.3.1: Swing Counter Program
Step 3: Click the Count button and see the number displayed in
the textfield changes. Every click on the Count button will
increase the number displayed in the textfield by 1. With your
output you are able to make the Swing Counter program on your
own. You have done this activity successfully. Great job and keep
it up!
The activity done will give a bird’s eye view on the capability of
Java in designing graphical user interface that will be shown on
the abstraction phase.
What is NetBeans?
Project WRITE XI: An Easy Guide for Course Pack making and Module Development
The most common IDE being used in Java programming is the
NetBeans. NetBeans is open-source. It began in 1996 as a Java
IDE student project at Charles University in Prague. It was
acquired by Sun Microsystems in 1999. In 2010, Oracle acquired
Sun including Netbeans. It provides seamless support for Java
Swing and bundled with an excellent profiler for performance
tuning.
1. Launch NetBeans.
2. From "File" menu ⇒ Choose "New Project...".
3. The "Choose Project" dialog pops up ⇒ Under
"Categories", choose "Java" ⇒ Under "Projects", choose
"Java Application" ⇒ "Next".
4. The "Name and Location" dialog pops up ⇒ Under
"Project Name", enter "FirstProject" ⇒ In "Project
Location", select a suitable directory to save your works
⇒ Uncheck "Use Dedicated Folder for Storing Libraries"
⇒ Uncheck "Create Main class" ⇒ Finish.
5. Right-click on "FirstProject" ⇒ New ⇒ Java Class
Project WRITE XI: An Easy Guide for Course Pack making and Module Development
(OR choose the "File" menu ⇒ "New File..." ⇒
Categories: "Java", File Types: "Java Class" ⇒ "Next").
6. The "Name and Location" dialog pops up ⇒ In "Class
Name", enter "Hello" ⇒ Delete the content in "Package"
if it is not empty ⇒ "Finish".
7. The source file "Hello.java" appears in the editor panel.
Enter the following codes:
What is GUI?
SwingXI:inAnJava is
Project WRITE a Graphical
Easy Guide for CourseUser
Pack Interface
making and(GUI) toolkit
Module that
Development
incorporates a rich set of widgets. It is a part of Java Foundation
Classes (JFC), which is an API for Java programs that provide
GUI. Swing includes packages that let you make a complex set of
GUI components for your Java applications and it is platform-
independent. You can use the Java GUI components like textbox,
buttons, and so forth from the Swing library and do not need to
create the components from scratch.
Week 2:
Project WRITE
// ThisXI:isAn
theEasy
startGuide
of theforJava
Course Packprogram
Swing making and Module Development
template
import java.awt.*; // Using layouts
import java.awt.event.*; // Using AWT event classes and listener
interfaces
import javax.swing.*; // Using Swing components and containers
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Exit the program when the close-window button clicked
setTitle("......"); // "super" JFrame sets title
setSize(300, 150); // "super" JFrame sets initial size
setVisible(true); // "super" JFrame shows
}
Now, kindly compare the Java Swing program template with the
Swing Counter program activity you have done earlier, side by
side. Notice that the two are very similar as the activity you have
done used the Java Swing program template. Now, look at each
section from the template with its corresponding comments and
review your activity that matches the specific sections. Your
activity program used codes that match the comments found in
the template. Below is the same set of codes that made your
activity program with similar comments to that of the Java Swing
program template.
cp.add(new
Project WRITE XI: An EasyJLabel("Counter"));
Guide for Course Pack making and Module Development
tfCount = new JTextField("0");
tfCount.setEditable(false);
cp.add(tfCount);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
Exit program if close-window button clicked
setTitle("Swing Counter"); // "super" JFrame sets title
setSize(300, 100); // "super" JFrame sets initial size
setVisible(true); // "super" JFrame shows
}
The first part of the code above calls the API package libraries to
use the different layouts, classes, interfaces, components, and
containers.
Project WRITE XI: An Then it creates
Easy Guide a subclass
for Course of Frame
Pack making andthat inherits
Module all the
Development
properties from Frame like content-pane and buttons. Then it
declares variables, creates a constructor to setup the GUI
components, and creates the main method.
The process below is about creating a basic calculator with GUI
implementation using the NetBeans IDE.
This tutorial teaches you how to create a simple graphical user
interface and add simple back-end functionality. It shows you
how to code the behavior of buttons and fields in a Swing form.
First will be about working through the layout and design of a
GUI and add few buttons and text fields. The text fields will be
used for receiving user input and also for displaying the program
output. The button will initiate the functionality built into the
front end. With the tutorial, you will be able to create a simple but
functional calculator application.
The first step is to create an IDE project for the application that
you are going to develop. You will name your
project NumberAddition.
1. Choose File > New Project. Alternatively, you can click
the New Project icon in the IDE toolbar.
2. In the Categories pane, select the Java node. In the
Projects pane, choose Java Application. Click Next.
3. Type NumberAddition in the Project Name field and
specify a path, for example, in your home directory, as the
project location.
4. (Optional) Select the Use Dedicated Folder for Storing
Libraries checkbox and specify the location for the
libraries folder.
5. Deselect the Create Main Class checkbox if it is selected.
6. Click Finish.
To proceed in creating your interface, you need to create a Java
container within which you will place the other required GUI
components. In here, you will create a container using
the JFrame component. You will place the container in a new
package, which will appear within the Source Packages node.
If you do not see the Palette window in the upper right corner of
the IDE, choose Window > Palette.
1. Start by selecting a Panel from the Swing Containers
category on Palette and drop it onto the JFrame.
2. While the JPanel is highlighted, go to the Properties
window and click the ellipsis (...) button next to Border to
choose a border style.
3. In the Border dialog, select TitledBorder from the list, and
type in Number Addition in the Title field. Click OK to
save the changes and exit the dialog.
4. XI:You
Project WRITE should
An Easy Guidenow see anPack
for Course empty titled
making andJFrame that says
Module Development
Number Addition like in the screenshot. Look at the
screenshot and add three JLabels, three JTextFields and
three JButtons as you see above.
Rename the Components
You are going to rename the display text of the components that
were just added to the JFrame.
1. Double-click jLabel1 and change the text property to First
Number:.
2. Double-click jLabel2 and change the text to Second
Number:.
3. Double-click jLabel3 and change the text to Result:.
4. Delete the sample text from jTextField1. You can make
the display text editable by right-clicking the text field and
choosing Edit Text from the popup menu. You may have
to resize the jTextField1 to its original size. Repeat this
step for jTextField2 and jTextField3.
5. Rename the display text of jButton1 to Clear. (You can
edit a button's text by right-clicking the button and
choosing Edit Text. Or you can click the button, pause,
and then click again.)
6. Rename the display text of jButton2 to Add.
7. Rename the display text of jButton3 to Exit.
Your Finished GUI should now look like the following
screenshot:
Project WRITE XI: An Easy Guide for Course Pack making and Module Development
3. Right-click the user name package and select New -> Java
Main Class...
Project WRITE XI: An Easy Guide for Course Pack making and Module Development
Application With the lesson you learned from the abstraction phase, you can
now apply it by creating your own application. To start off, use
the activity code with similar comments to that of the Java Swing
program template then change by adding and removing parts of it
as presented in the later tutorial to create a program that will let
the user input more than just numbers then display its intended
output serving as generated report depending on what use you can
think of it.
You now have the knowledge and skill that you can confidently
use to use the specific IDE tool to design graphical user interface
for whatever application is needed to be programmed. The end of
this lesson should be the start or continuation to your journey into
Module 5: File Handling and Database Connectivity.
About the JFC and Swing. (n.d.). Retrieved July 20, 2020, from
https://docs.oracle.com/javase/tutorial/uiswing/start/about.h
tml
MODULE ASSESSMENT
Using the lessons learned in the development of package, application programming interface,
and graphical user interface, create a program that will allow the user to graphically input the
user’s profile and be displayed in a table view.
MODULE SUMMARY
This module teaches theWRITE
Project development
XI: An Easyof package,
Guide application
for Course programming
Pack making interface, and
and Module Development
graphical user interface.
//Package
//API
The GUI needs the appropriate API such as the Java Swing to provide graphical user interface
without making it from scratch. GUI helps the user to navigate the program more effectively and
more conveniently. IDEs increases programmer productivity by combining common activities of
writing software into a single application including the editing of source code, syntax
highlighting, autocomplete, building executables, and debugging. The most common IDE being
used in Java programming is the NetBeans. NetBeans is open-source.
Project WRITE XI: An Easy Guide for Course Pack making and Module Development