ghanoz 2480 - menggunakan swing look&feel nimbus

6

Click here to load reader

Upload: muhammad-ghazali

Post on 11-Jun-2015

205 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

Penulis : M. Ghazali a.k.a ghaNOZ 2480

Penerbit : TheONEmAn2480

Tahun Terbit : 2008

Menggunakan Swing Look&Feel Nimbus

Pendahuluan

Pada tutorial ini saya akan menunjukkan perbedaan tampilan GUI (Graphical User Interface)

antara program java yang menggunakan Default look and feel dengan Nimbus look and feel

yang merupakan look and feel jenis baru yang cross-platform yang terdapat pada Java SE

Update 10 Beta.

Kenapa Nimbus?

Nimbus menyediakan tampilan yang lebih halus (polished look) pada aplikasi yang

menggunakannya. Dan karena Nimbus digambar secara keseluruhan dengan menggunakan

Java 2D vector graphics, daripada bitmap statis (static bitmap), dan karena ukurannya kecil

hanya 56 KB (Kilo Bytes) dan dapat di-render pada resolusi yang berubah-ubah.

Perbedaan Tampilan

Pada gambar di bawah ini ditunjukkan program yang menggunakan Default look and feel atau

juga disebut Metal look and Feel.

1

Page 2: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

Gambar 1 – Menggunakan Metal look and Feel

Tapi sekarang kita ganti look and feel-nya dengan menggunakan Nimbus bandingkan dengan

tampilan program yang tadi.

Gambar 2- Menggunakan Nimbus Look and Feel

Atau agar lebih jelas perbedaannya kita lihat gambar di bawah ini:

2

Page 3: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

Gambar 3 - Metal Look and Feel

Gambar 4 - Nimbus Look and Feel

3

Page 4: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

Bagaimana Menggunakannya?

Untuk menggunakan Nimbus Look and Feel, kita panggil method setLookAndFeel pada class

UIManager.

Potongan kode yang digunakan dalam program try{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAn

dFeel");

} catch (UnsupportedLookAndFeelException e) {

// handle exception

} catch (ClassNotFoundException e) {

// handle exception

} catch (InstantiationException e) {

// handle exception

} catch (IllegalAccessException e) {

// handle exception

}

Contoh Program

Bila anda penasaran cobalah program di bawah ini.

NimbusSwing.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class NimbusSwing extends JFrame implements

ActionListener

{

private Container c;

private JButton clickMe;

//--> constructor

public NimbusSwing()

{

// used Nimbus Look&Feel

4

Page 5: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.Nimbu

sLookAndFeel");

}

catch(UnsupportedLookAndFeelException e)

{

// handle exception

}

catch (ClassNotFoundException e)

{

// handle exception

}

catch (InstantiationException e)

{

// handle exception

}

catch (IllegalAccessException e)

{

// handle exception

}

createComponents();

} // end of constructor

//--> createComponents method

// create the GUI components

private void createComponents()

{

c = getContentPane();

setTitle("Using Nimbus [Swing Look&Feel]");

setBounds(100, 100, 150, 150);

c.setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create the clikMe button

clickMe = new JButton("Click Me");

5

Page 6: ghaNOZ 2480 - Menggunakan Swing Look&Feel Nimbus

TheONEmAn2480

clickMe.setLocation(40, 40);

clickMe.setSize(clickMe.getPreferredSize());

// add the action listener to object

clickMe.addActionListener(this);

// add the object to the container

c.add(clickMe);

} // end of createComponents

//--> actionPerformed method

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource().equals(clickMe))

{

JOptionPane.showMessageDialog(this, "Message",

"This what you got, when you used Nimbus Swing Look&Feel",

JOptionPane.INFORMATION_MESSAGE);

}

} // end of actionPerformed

//--> start point

public static void main(String args[])

{

NimbusSwing aplikasi = new NimbusSwing();

aplikasi.setVisible(true);

} // end of main method

} // end of class NimbusSwing

Referensi

1. “Nimbus”, http://java.sun.com/javase/downloads/ea/6u10/nimbus.jsp

6