You are on page 1of 23

GUI

WEEK 3
Pemrograman Terdistribusi
Teknik Informatika - Multimedia
Universitas Surabaya
What is GUI

GUI (Graphical User Interface) is useful for user to interact better with the
program.

User interacts with various graphical elements such as button, Message dialog
box, menu, and etc.

JAVA provide two graphical libraries, AWT and Swing.


AWT vs SWING
AWT stands for Abstract Windowing Toolkit.

Prior to Swing, AWT was used to develop GUI and rich client interface

But AWT had one major problem. AWT was platform dependent, which means a
program written in AWT behaves differently in different platforms.

Hence it defeats WORA (Write Once, Run Anywhere) purpose which is the key
Java philosophy.
Swing
Swing on the other hand is purely (100%) written in Java.

A swing application developed on one platform behaves the same on any other
platform in which Java is installed.

Hence today almost all Java programmers prefer Swing over AWT for GUI
development.
Exercise 1: Simple Calculator
Create Project
Create a new Java Application Project and give a name “GUIExcercise”

Add a JFrame Form in GUIExercise packages and give a name


“SimpleCalculator”
Area where AWT
and Swing
component are
placed

Palette (Simiar to ToolBox in


Visual Studio)
Design The UI
Add the followong components (drag & drop from Palette)

JTextField
Font: Lucinda Grande 24
HorizontalAligment:
Center

jButton

jLabel
Change the variable
JTextField name of
Font: Lucinda Grande 24
Enabled: Uncheck
components
HorizontalAligment: Center
Change Fonts and Disable TextField

Change the text font Disabling TextField by


at Properties Tab unchecking Enabled Option
Attach Event Handler
To create an event handler for AWT or Swing components, just double click on
the desired component (for example: jButton).

Add the code inside the action methods according to the case. For example: the
"+" button is used to add 2 numbers entered by the user.

See the next Slide


Button Add Codes

Get value from


jTextField
Convert string to
double
Set value to
jTextField
Alternative Way to Attach Event Handlers
Another event handlers can be
selected by right clicking the awt or
swing components -> events.
Change Default MainClass
Set the main class to
SimpleCalculator (jFrame).
So when project is run,
SimpleCalculator.java will be
executed.

Right click on GUIExercise


project -> Properties -> Run
MessageBox
Users should not input any characters in
jTextField.

The program should show the message to


the users.
Create Message Box
Di Java, perintahnya adalah
JOptionPane.showMessageDialog(Component component, Object message);

● component adalah frame induk dari message box yang dimunculkan\


● Pada kebanyakan kasus, menggunakan keyword this saja cukup
● message adalah teks yang ingin ditampilkan
Message Box
You can explore MessageDialog more at the following link:
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Exercise 2: Employee Payroll System
Create New Project
Instructions
Create a program to calculate the total salary of employee.

The salary of Full Time Employee is the


main salary + 10% of main salary x Number of Children + 1% of main salary
x Working Experience (in Years).

The salary of Part Time Employee is the


main salary + Transportation Cost + 1% of main salary x working days in a
month.

Working Experience is in years.

Working Days is number of working days in a month.


Hints!
FullTimeEmployee and PartTimeEmployee class inherit all stuff from Employee
Class

Make Employee class as an abstract class (remember the abstract class concept)
Submit your work!
ZIP and upload your works to ULS!

You might also like