JAVA GRAPHICAL
USER INTERFACE
(GUI)
Graphical User Interface
(GUI)
• Adalah fasilitas yang disediakan java untuk
membuat program dengan tampilan yang lebih
menarik dan user friendly.
• Fasilitas yang digunakan adalah komponen Java
Swing yang merupakan bagian dari Java API
(Application Programming Interface).
• Komponen Java Swing tersebut terdapat dalam
package [Link] yang dapat digunakan
dengan perintah import
Import [Link]
Komponen Swing
• Top Level Container
• Intermediate Container
• Atomic Component
• Layout Manager
• Event Handling
Top Level Container
• Merupakan container dasar dimana
komponen lainnya diletakkan.
• Contohnya Frame, dialog dan applet
• Diimplementasikan berupa class
JFrame, JDialog dan JApplet
Intermediate Container
• Merupakan container perantara dimana
komponen lainnya akan diletakkan.
• Contohnya pane
• Diimplementasikan dengan class JPane
Atomic Component
• Merupakan komponen yang mempunyai
fungsi spesifik dan komponen ini dapat
berinteraksi langsung dengan user.
• Contohnya Button, Label, Text field dan
Text area.
• Diimplementasikan dengan class
JButton, JLabel,JTextField, JTextArea
Layout Manager
• Berfungsi untuk mengatur tata letak
atau posisi komponen didalam
container.
• Diimplementasikan dengan class
BorderLayout, BoxLayout, FlowLayout,
GridBagLayout dan GridLayout
Event Handling
• Menangani handling yang dilakukan
oleh user, seperti menekan tombol,
memperbesar atau memperkecil ukuran
frame, mengklik mouse dsb.
Dialog Box
• Dialog Box termasuk Top Level
Container yang sering digunakan.
• Diimplementasikan dengan class
JOptionPane
Contoh Pemakaian
Dialog Box
1 // Fig. 2.6: [Link]
2 // Printing multiple lines in a dialog box.
3
4 // Java packages
5 import [Link]; // program uses JOptionPane
6
7 public class Welcome4 {
8
9 // main method begins execution of Java application
10 public static void main( String args[] )
11 {
12 [Link](
13 null, "Welcome\nto\nJava\nProgramming!" );
14
15 [Link]( 0 ); // terminate application with window
16
17 } // end method main
18
19 } // end class Welcome4
Output
• Baris 1-2: adalah komentar
4
Keterangan Program
// Java packages
• Terdapat 2 jenis packages dalam Java API
• Cores Packages
• Dimulai dengan kata java
• Terdapat didalam Java 2 Software Development Kit
• Extension packages
• Dimulai dengan javax
5 import [Link]; // program uses OptionPane
• Deklarasi import
• Memberitahu compiler untuk memanggil class
JOptionPane dari [Link] package
Keterangan Program
• Nama class Welcome4 and main
12 [Link](
13 null, "Welcome\nto\nJava\nProgramming!" );
• Memanggil method showMessageDialog yang terdapat
dalam class JOptionPane
• Diikuti oleh dua arguments
• Masing-masing arguments dipisahkan dengan koma
• Pada contoh ini argument pertama selalu null
• Argument kedua adalah string untuk dicetak
• showMessageDialog adalah static method
dari class JOptionPane
• static methods dipanggil dengan menggunakan nama
class diikuti titik (.) kemudian nama method
1 // Fig. 2.9: [Link]
2 // Addition program that displays the sum of two numbers.
3
4
Contoh Input data
// Java packages
dengan Dialog Box
5 import [Link]; // program uses JOptionPane
6
7 public class Addition {
8
9 // main method begins execution of Java application
10 public static void main( String args[] )
11 {
12 String firstNumber; // first string entered by user
13 String secondNumber; // second string entered by user
14
15 int number1; // first number to add
16 int number2; // second number to add
17 int sum; // sum of number1 and number2
18
19 // read in first number from user as a String
20 firstNumber = [Link]( "Enter first integer" );
21
22 // read in second number from user as a String
23 secondNumber =
24 [Link]( "Enter second integer" );
25
26 // convert numbers from type
String to type int
27 number1 =
[Link]( firstNumber );
28 number2 =
[Link]( secondNumber );
33 // display result
34 29 [Link](null, "The sum is " + sum,
35 30 // JOptionPane.PLAIN_MESSAGE
"Results", add numbers );
36
37
31 [Link](sum = number1
0 );
+ number2;
// terminate application with window
38 32
39 } // end method main
40
41 } // end class Addition
package luaspp;
import [Link];
public class Main {
public static void main(String[] args) {
String p1,l1;
int p,l,L;
p1= [Link]("Masukan Panjang : ");
l1= [Link]("Masukan Lebar : ");
p=[Link](p1);
l =[Link](l1);
L=p*l;
[Link](null,"Luas Persegi Panjang Adalah : " +
L, "Hasil",JOptionPane.WARNING_MESSAGE);
[Link](0);
}}
• import [Link]; // program uses JOptionPane
• public class Luassegitiga {
• // main method begins execution of Java application
• public static void main( String args[] )
• {
• String alas; // first string entered by user
• String tinggi; // second string entered by user
• //int a1; // first number to add
• // int t2; // second number to add
• double luas,a1,t2; // sum of number1 and number2
• [Link](
• null, "Welcome\nto\nJava\nProgramming!" );
• // read in first number from user as a String
• alas = [Link]( "masukkan alas integer" );
• // read in second number from user as a String
• tinggi =
• [Link]( "masukkan tinggi integer" );
• // convert numbers from type String to type int
• a1 = [Link]( alas );
• t2 = [Link]( tinggi );
• // add numbers
• luas =(0.5*a1*t2);
• // display result
• [Link](null, "Luas segitiga " + luas,
• "Results", JOptionPane.PLAIN_MESSAGE );
• [Link]( 0 ); // terminate application with window
• } // end method main
• } // end class Addition
2.5 Another Java
Message dialog type Ic on Desc ription
Application: Adding Integers
JOptionPane.ERROR_MESSAGE Displays a dialog that indicates an error
to the user.
JOptionPane.INFORMATION_MESSAGE Displays a dialog with an informational
message to the user. The user can simply
dismiss the dialog.
JOptionPane.WARNING_MESSAGE Displays a dialog that warns the user of a
potential problem.
JOptionPane.QUESTION_MESSAGE Displays a dialog that poses a question to
the user. This dialog normally requires a
response, such as clicking on a Yes or a
No button.
JOptionPane.PLAIN_MESSAGE no icon
Displays a dialog that simply contains a
message, with no icon.
Fig. 2.12 JOptionPane c onstants for message dialogs.
Operator Aritmatika
Operator(s) Operation(s) Order of evaluation (prec edenc e)
* MultiplicationEvaluated first. If there are several of this type
/ Division of operator, they are evaluated from left to
% Remainder right.
+ Addition Evaluated next. If there are several of this type
- Subtraction of operator, they are evaluated from left to
right.
Fig. 2.17 Prec edenc e of arithmetic operators.
Operator Relasi
Standard algebraic J ava equality Example Meaning of
equality or or relational of J ava J ava condition
relational operator operator condition
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
<= x <= y x is less than or equal to y
Fig. 2.19 Equality and relational operators.
Contoh program
1 // Fig. 2.20: [Link]
2 // Compare integers using if statements, relational operators
3 // and equality operators.
menggunakan operator
4
5 // Java packages
6 import [Link];
7
8 public class Comparison {
9
10 // main method begins execution of Java application
11 public static void main( String args[] )
12 {
13 String firstNumber; // first string entered by user
14 String secondNumber; // second string entered by user
15 String result; // a string containing the output
16
17 int number1; // first number to compare
18 int number2; // second number to compare
19
20 // read first number from user as a string
21 firstNumber = [Link]( "Enter first integer:" );
22
23 // read second number from user as a string
24 secondNumber =
25 [Link]( "Enter second integer:" );
26
27 // convert numbers from type String to type int
28 number1 = [Link]( firstNumber );
29 number2 = [Link]( secondNumber );
30 // initialize result to empty String
32 result = "";
34 if ( number1 == number2 )
35 result = result + number1 + " == " + number2;
36
37 if ( number1 != number2 )
38 result = result + number1 + " != " + number2;
39
40 if ( number1 < number2 )
41 result = result + "\n" + number1 + " < " + number2;
42
43 if ( number1 > number2 )
44 result = result + "\n" + number1 + " > " + number2;
45
46 if ( number1 <= number2 )
47 result = result + "\n" + number1 + " <= " + number2;
48
49 if ( number1 >= number2 )
50 result = result + "\n" + number1 + " >= " + number2;
51
52 // Display results
53 [Link]( null, result, "Comparison Results",
54 JOptionPane.INFORMATION_MESSAGE );
55
56 [Link]( 0 ); // terminate application
57
58 } // end method main
59
60 } // end class Comparison