Pemrograman Berorientasi Objek Lanjut Lecture 01

advertisement
Pemrograman Berorientasi Objek
Lanjut
Lecture 01 - Introduction
NIKO IBRAHIM, MIT
PROGRAM STUDI SISTEM INFORMASI
FAKULTAS TEKNOLOGI INFORMASI
UNIVERSITAS KRISTEN MARANATHA
Tujuan Perkuliahan PBO Lanjut
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
dalam pemrograman berorientasi objek.
1.
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)
 Bila kena cekal, tidak diijinkan ikut UAS  E
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
 Database access and processing
Jadwal Kuliah
Session
Session 01
Session 02
Session 03
Session 04
Session 05
Session 06
UTS
Session 07
Session 08
Session 09
Session 10
Session 11
Session 12
UAS
Lectures
Materials
Pengenalan OOP Lanjut & Swing
Komponen-komponen Swing Dasar 1
Komponen-komponen Swing Dasar 2
Layout Manager
Event-Driven Programming
NetBeans GUI Builder Tutorial
Java Collection Framework
Database & JDBC
Database & JDBC
Database & Java Persistence API (JPA)
Database & Java Persistence API (JPA)
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
Mark = 0, for all
practical works
Mark = 0, for all
quizzes
1
Programming tasks
10
20%
2
Quizzes
5
10%
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
2.
3.
4.
5.
Ed., Y. Daniel Liang
Learning Java, Jonathan Knudsen & Patrick
Niemeyer, O’Reilly, 2005. (Main Reading
Materials)
Java Swing, Matthew Robinson & Pavel Vorobiev,
Manning, 2005.
Java API Documentation, Sun Microsystems
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)
 Email me at: [email protected]
Review: HelloMe.java
0: // Exercise 1: HelloMe.java
1: // 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
Line 4: instruksi pada komputer untuk menulis suatu String (kata-kata)
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;
}
Interitance: copying
property/method of
the parent class
Property -->private
Constructor -->public, no
returned value
Method --> public
Belongs to the object/instance
public static void main (String args[ ]) {
SegiEmpat s = new SegiEmpat(3,4);
System.out.println("Luas: " + s.luas());
}
}
Static Method
Belongs to the class
Materi Hari Ini
•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 elemenelemen tersebut?
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
radio
button
frame
checkbox
label
button
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
relative layouts.
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)
3. appropriate action taken
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 (an extension)
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

avoid mixing with AWT.
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):
 Swing component names start from J.
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
display the message Hello, World! in its title bar.
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:

Example: frame.setLocationRelativeTo(null);
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
JLabel label1 = new JLabel("Hello, World!");
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();
button1.setText("Click me!");
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
That’s all for today!
YOU’VE LEARNED:
•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
To Do Today
SKIMMING JAVA DOC 7
PRAKTIKUM 01 – PENGENALAN JAVA SWING
Study Guide
TO DO THIS WEEK:
• Read: Java Review  Diktat OOPL, Page 125
• Read: Java swing 2, O’Reilly (chapter 1)
• Run the Demo ‘SwingSet2.jar’
• Study the source code of ‘SwingSet2’
• Go to
http://java.sun.com/docs/books/tutorial/uiswing /
Download