N I K O I B R A H I M , M I T
P R O G R A M S T U D I S I S T E M I N F O R M A S I F A K U L T A S T E K N O L O G I I N F O R M A S I U N I V E R S I T A S K R I S T E N M A R A N A T H A
Pemrograman Berorientasi Objek
Lanjut
Tujuan Perkuliahan PBO Lanjut
1. Mahasiswa mampu membuat program aplikasi
Java berdasarkan konsep objek oriented.
2. Mahasiwa mampu membuat program aplikasi yang
berhubungan dengan database.
3. Mahasiswa memahami konsep-konsep lanjutan
Rules
Pedoman Perkuliahan Titip absen E
Handphone wajib di-nonaktifkan/silent
Menggunakan pakaian rapi & bersepatu
Kewajiban hadir:
Kehadiran > 15 menit, tidak boleh masuk kelas
75% kehadiran dosen (<= 25% berarti cekal)
Pedoman Penilaian
NA= 30% UTS + 30% UAS + 15% Quiz + 25%
Praktikum Penghitungan nilai: A: 80 <= NA <= 100 B+: 73 <= NA < 80 B: 67 <= NA < 73 C+: 61 <= NA < 67 C: 55 <= NA < 61 D: 41 <= NA < 55 E: NA <41
Materi Keseluruhan
Java Swing
Komponen-komponen Swing Layout Manager
Event-driven programming
Generics & Collections File & Stream processing
Jadwal Kuliah
Session Lectures
Materials
Session 01 Pengenalan OOP Lanjut & Swing
Session 02 Komponen-komponen Swing Dasar 1
Session 03 Komponen-komponen Swing Dasar 2
Session 04 Layout Manager
Session 05 Event-Driven Programming
Session 06 NetBeans GUI Builder Tutorial
UTS
Session 07 Java Collection Framework
Session 08 Database & JDBC
Session 09 Database & JDBC
Session 10 Database & Java Persistence API (JPA)
Session 11 Database & Java Persistence API (JPA)
Session 12 Presentasi Tugas
Details of Assessable Work in The Topic
No Details about each form of
assessable work in the topic Amount
Proportion of total mark
Penalties to be applied for plagiarism 1 Programming tasks 10 20% Mark = 0, for all
practical works
2 Quizzes 5 10% Mark = 0, for all
quizzes
3 Assignments 2 10% Mark = 0
4 Mid-semester Exam 1 25% Mark = 0, for all exams 5 Final-semester Exam 1 25% Mark = 0, for all exams
Reading Materials
1. INTRODUCTION TO JAVA PROGRAMMING, 10th
Ed., Y. Daniel Liang
2. Learning Java, Jonathan Knudsen & Patrick
Niemeyer, O’Reilly, 2005. (Main Reading Materials)
3. Java Swing, Matthew Robinson & Pavel Vorobiev,
Manning, 2005.
4. Java API Documentation, Sun Microsystems 5. NetBeans Tutorials and Documentations
Softwares and Tools
JDK 1.7 + Java API Documentation
Editors:
JGrasp (Simple Java editor, free download) NetBeans 7 (Java IDE, free download)
Got Any Problems?
Ask directly after the class! (preferable)
Review: HelloMe.java
0: // Exercise 1: HelloMe.java1: // NIK: 730015, NAMA: Niko Ibrahim 2: public class HelloMe {
3: public static void main (String args[ ]) {
4: System.out.println("Hello World, Niko!");
5: }
6: }
Line 0: Komentar program, judul program, keterangan penting. Line 1: Selalu tuliskan identitas penulis program.
Line 2: - Nama kelas. Setiap program Java minimal memiliki 1 deklarasi kelas. - Kelas diawali huruf kapital untuk setiap kata.
- Untuk men-save public class ke file, harus diberi nama sesuai dengan nama kelas
tsb dan diakhiri dengan ekstensi .java. Tentang public akan dibahas nanti.
Line 3: main method the starting point of every Java application
Review OOP
public class SegiEmpat extends Shape{
private int tinggi;
private int lebar;
public SegiEmpat(int h, int w){ this.tinggi = h;
this.lebar = w; }
public int luas (){ return h*w;
}
public static void main (String args[ ]) { SegiEmpat s = new SegiEmpat(3,4);
System.out.println("Luas: " + s.luas()); } } Property -->private Constructor -->public, no returned value Method --> public
Belongs to the object/instance
Static Method Belongs to the class
Interitance: copying property/method of
•J A V A A W T V S J A V A S W I N G
•K O M P O N E N - K O M P O N E N D A S A R S W I N G •L A T I H A N
3 hal penting dalam pemrograman GUI
1. Elemen-elemen (components) apa yang bisa
ditampilkan di layar?
2. Bagaimana cara kira menyusun dan
menempatkan elemen-elemen tersebut?
3. Bagaimana kita berinteraksi dengan
No 1. Components
Parts of GUI: buttons, menus, checkboxes, sliders,
text fields, labels, and so on.
API provides numerous implementations
We can create own custom components
frame button radio button checkbox label
No 2. Layouts
Susunan komponen di layar:
Absolute: x, y coordinates specified for each component (rarely used)
Relative with respect to other component positions/sizes, screen resolution,
fonts used etc.
Layout Managers (objects) are used to deal with
No 3. Event Handling
A technique to deal with user input Event-based model:
1. button clicked (by user)
2. event generated (by the system)
Java Solution: Swing
Untuk melakukan GUI Programming, Java
menyediakan 2 libraries:
AWT (Abstract Window Toolkit), and Swing
AWT - an old library, present in the first Java
release,
Swing - an improved library added later
Swing does not replace AWT , it builds on top of
AWT vs Swing
AWT components are heavyweight peer-based, platform-dependant implementation (in native
code).
Difficult to port, platform-dependant look.
Swing components are pure Java applications lightweight - do not use peers
Pluggable look-and-feel - platform-independant, programmer
designed look (system independent).
Whenever possible Swing components should be
used
AWT vs Swing (2)
Highest level components are heavy
Each light one has a corresponding heavy one,
somewhere in the hierarchy (a background to draw onto):
Java API (JavaDoc)
Sebagai programmer, JAVA API adalah suatu
dokumen yang wajib dimiliki.
Semua class & method (termasuk untuk Java Swing)
ada di dalam dokumen ini!
Dokumentasi, Tutorial, Contoh-contoh (termasuk
aplikasi Swing):
http://www.netbeans.org/kb/index.html http://docs.oracle.com/javase/7/docs/api/
Komponen Swing Dasar
JFrame
JPanel JLabel JButton
Frame
The top-level component for most Swing-based
applications is called a frame and is defined by the
JFrame class.
By itself, a frame doesn’t do much.
But to do anything else in Swing, you must first
create a frame.
The figure shows a frame that does nothing but
Exercise 01: HelloFrame.java
import javax.swing.*;public class HelloFrame extends JFrame {
/** Creates a new instance of HelloFrame */
public HelloFrame() {
this.setSize(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Hello World!");
this.setVisible(true);
}
public static void main(String[] args) { new HelloFrame();
} }
Positioning the Frame On-Screen
If you want to place the frame at some arbitrary
location on-screen, use the setLocation method.
Example: frame.setLocation(0,0);
If you want to center the frame on-screen, call the
setLocationRelativeTo method and pass null as the parameter:
JLabel
A label is a component that simply displays text. Labels are used for a variety of purposes:
to display captions for other controls such as text fields or combo
boxes,
to display informational messages, or
to show the results of a calculation or a database lookup.
A label can also display an image, or it can display both
an image and some text. And we have complete control over the appearance of the text
We can specify the font, size, whether the text is bold,
italic, or underlined, what color the text is displayed as, and so on.
Example:
JLabel label1 = new JLabel(); label1.setText("Hello, World!"); or
JButton
Next to labels, the Swing component you use most
is the JButton component, which creates a button the user can click.
Example:
JButton button1 = new JButton("Click me!");
or
JButton button1 = new JButton();
JPanel
A panel is a type of container that’s designed to hold a
group of components so they can be displayed on a frame.
The normal way to display a group of controls such as
text fields, labels, buttons, and other GUI widgets is to add those controls to a panel, and then add the panel to the frame.
Example:
class HelloPanel extends JPanel{ public HelloPanel(){
JLabel label1 = new JLabel("Hello, World!"); JButton button1 = new JButton("Click me!");
} }
Then, in the frame class contstructor, we create a new
instance of the panel class and add it to the frame: frame.add(new HelloPanel());
Putting it all together
Exercise 02: HelloFrame2.java
import javax.swing.*;
public class HelloFrame2 extends JFrame { /** Creates a new instance of HelloFrame */
public HelloFrame2() {
this.setSize(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Hello World!");
HelloPanel panel1 = new HelloPanel(); this.add(panel1);
this.setVisible(true);
this.setLocationRelativeTo(null); // set location to the center
}
class HelloPanel extends JPanel{
public HelloPanel(){
// code to add components to the panel goes here
JLabel label1 = new JLabel("Hello, this is label!"); this.add(label1);
JButton button1 = new JButton("Click me!"); this.add(button1);
} }
public static void main(String[] args) { new HelloFrame2();
} }
FULL CODE: Copy & paste the codes into JGrasp
Y O U ’ V E L E A R N E D : •R E V I E W O O P : • class, • object/instance, • property, • method, • constructor •I N T R O D U C T I O N T O S W I N G :
• JFrame, JPanel, JLabel, JButton
S K I M M I N G J A V A D O C 7
P R A K T I K U M 0 1 – P E N G E N A L A N J A V A S W I N G
To Do Today
T O D O T H I S W E E K : • R e a d : J a v a R e v i e w D i k t a t O O P L , P a g e 1 2 5 • R e a d : J a v a s w i n g 2 , O ’ R e i l l y ( c h a p t e r 1 ) • R u n t h e D e m o ‘ S w i n g S e t 2 . j a r ’ • S t u d y t h e s o u r c e c o d e o f ‘ S w i n g S e t 2 ’ • G o t o h t t p : / / j a v a . s u n . c o m / d o c s / b o o k s / t u t o r i a l / u i s w i n g /