creating graphical user interfaces [1] with javadinus.ac.id/repository/docs/ajar/10-guipart1.pdf ·...

57
Konten The Mathematical Concept of GUIs Creating Graphical User Interfaces [1] with Java By: De Rosal Ignatius Moses Setiadi APIs for Java GUI Swing GUI Components Basic Components Interactive Displays of Highly Formatted Information Uneditable Information Displays Top-Level Containers General-Purpose Containers Special- Purpose Containers Anatomy of GUI Application Using Swing Components Tugas

Upload: lykiet

Post on 09-Aug-2019

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Konten

The Mathematical

Concept of GUIs

Creating Graphical User Interfaces [1]

with JavaBy: De Rosal Ignatius Moses Setiadi

APIs for Java GUI

Swing GUI Components

Basic

Components

Interactive

Displays of Highly Formatted

Information

UneditableInformation

Displays

Top-Level Containers

General-Purpose Containers

Special-

Purpose Containers

Anatomy of GUI

Application

Using Swing Components

Tugas

Page 2: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

• The Mathematical Concept of GUIs

• GUI Concept in Java

• Using Swing Components

–JFrame

–JLabel

Abdul Kadir, Pemrograman Java 2 (Bab 28 Halaman 601)

Page 3: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Layar Komputer

• Memiliki 2 koordinat

–Horisontal (sumbu x)

–Vertikal (sumbu y)

• Titik awal ada pada (x=0, y=0) pada kiri atas layar

• Jarak diukur dalam pixel

–dengan sumbu x koordinat meningkat ke sebelah kanan

–dan sumbu y koordinat meningkat ke bawah

Page 4: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Standard Algebraic Coordinate System

Standard Computer-Screen Coordinate System

Page 5: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh• A window:

– x = 150,

– y = 325,

– width = 600,

– height = 350

• Its contains a button:

– x = 125,

– y = 125,

– width = 300,

– height = 100

325

125

150

125100

300

350

600

Sumbu X

Sum

buY

incr

easi

ng d

ownw

ards

increasing rightwards

WindowBounds=(150,325,600,350)

ButtonBounds=(125,125,100,300)

Bounds=( x, y, width, height)

Page 6: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

AWT (Abstract Window Toolkit )

• Merupakan komponen GUI (java.awt) yang pertamakali dikenalkan oleh Sun Java.

• Penggunaan komponen GUI AWT sudah tidakdirekomendasikan untuk development baru.

Page 7: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Swing or JFC (Java Foundation Class):

• Merupakan library dan komponen GUI (javax.swing) versi baru dan direkomendasikan untuk development GUI programming saat ini.

• Komponen Swing components banyak diturunkan dariAWT and lebih lengkap dari AWT

Page 8: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

• Basic Components

• Interactive Displays of Highly Formatted Information

• Uneditable Information Displays

• Top-Level Containers

• General-Purpose Containers

• Special-Purpose Containers

Page 9: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JButton

Page 10: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JCheckBox

Page 11: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JList

Page 12: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JComboBox

Page 13: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JMenu

Page 14: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JRadioButton

Page 15: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JSpinner

Page 16: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JTextField

Page 17: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JPasswordField

Page 18: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JSlider

Page 19: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JColorChooser

Page 20: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JEditorPane

Page 21: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JTextArea

Page 22: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JTable

Page 23: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JTree

Page 24: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JFileChooser

Page 25: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JProgressBar

Page 26: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JLabel

Page 27: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JToolTip

Page 28: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JSeparator

Page 29: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JFrame

Page 30: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JDialog

Page 31: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JApplet

Page 32: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JPanel

Page 33: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JScrollPane

Page 34: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JSplitPane

Page 35: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JTabbedPane

Page 36: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JToolBar

Page 37: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JInternalFrame

Page 38: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JlayeredPane

Page 39: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Rootpane

Page 40: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Internal structure

JPanel

JButton

JFrame

JLabel

JFrame

JPanel

JButton JLabel

containers

Component

Page 41: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JFrame (Top-Level Containers)

• Sebuah frame biasanya merupakan bagaian dari

• JFrame merupakan Top-Level Containers dalamkomponen swing.

• JFrame menyediakan atribut dasar dan perilakumisalnya, title bar dan tombol untuk

minimaze, maximize dan close).

Page 42: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JFrame [Metode]Metode Jframe yang sering digunakan:

• void setSize (int x, int y) mengatur ukuran frame

• void setDefaultCloseOperation(int operation) mengatur caramenutup frame

• void setLocation(int x, int y)mengatur lokasi frame

• void setLocationRelativeTo(Component c) mengatur lokasi frame dari komponen tertentu

• void setResizable(boolean b) menentukan dapat diubahukurunya atau tidak

• void setVisible (boolean b) menentukan dapat dilihat atau tidak

• void setBounds(int x, int y, int width, int height) kombinasisetLocation dan setSize

Page 43: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh Kode Membuat JFrame [dengan Metode]

Set title of Frame

Page 44: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Metode setDefaultCloseOperation

• EXIT_ON_CLOSE quits the application;

• DO_NOTHING_ON_CLOSE does nothing;

• HIDE_ON_CLOSE makes the window invisible but keeps it in memory for redisplay;

• DISPOSE_ON_CLOSE destroys the window and frees up associated memory.

Page 45: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh Kode Membuat JFrame [dengan

Konstruktor]

Page 46: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Menambahkan Konten pada JFrame• Dibutuhkan untuk menampilkan konten dari

window

– Container adalah wadah untuk komponen2 lain.

• Kelas Container terdapat dalam AWT

– Tersedia jika kita melakukan import kelas AWT

– import java.awt.*;

• Untuk membuat instan dari kelas kontiner, gunakan:

– kata kunci

– Dan metode getContentPane( )

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html

Page 47: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh Kode Menambahkan Konten JLabel

Page 48: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh Kode Menambahkan Konten Jlabel [2]

Page 49: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Contoh Kode Menambahkan Konten Jlabel [2]lanjut

Page 50: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

JLabel

Adalah area untukmenampilkantulisan pendekatau gambar

Page 51: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Bagaimana menambahkan 2 konten?

Page 52: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Hasil MyFrame5.java

Page 53: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Jlabel - Constructor

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JLabel.html

Page 54: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Menampilkan Teks dan Gambar dengan JLabel

Page 55: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Hasil MyFrame6.java

Page 56: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Tugas 1

• Buat tampilan seperti dibawah ini:

• Clue: Gunakan layout

Page 57: Creating Graphical User Interfaces [1] with Javadinus.ac.id/repository/docs/ajar/10-GUIpart1.pdf · •The Mathematical Concept of GUIs •GUI Concept in Java •Using Swing Components

Tugas 2

• Buat tampilan seperti dibawah ini:

• Clue: Gunakan ImageIcon