posted Nov 5, 2011, 6:45 PM by Editor KursusInternet
[
updated Nov 5, 2011, 9:21 PM
]
Pendahuluan
Warna selalu memegang peranan penting dalam perancangan user interface. Seperti untuk highlight atau warning untuk beberapa hal tertentu.
Untuk pewarnaan widget pada SWT, kita dapat menggunakan method setForeground() dan setBackground() dengan parameter objek Color seperti dijelaskan pada bagian berikut.
Setter Method dan Class Color
Syntax dari kedua method setter, yaitu setForeground dan setBackground() tersebut di atas cukup sederhana, seperti ditunjukkan sebagai berikut :
setBackground(Color col)
setForeground(Color col)
Sedangkan objek Color yang diperlukan dapat diinisialisasi dengan variasi constructor sebagai berikut :
Color(Display display, int red, int green, int blue)
Color(Display display, RGB rgb)
Untuk memperjelas penggunaannya, berikut langsung kita gunakan contoh dan hasil eksekusinya.
Contoh Penggunaan
WarnaWidget.javapackage com.kursusinternet.swt;
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell;
public class WarnaWidget { Display display; Shell shell;
public void tampil() { display = new Display(); shell = new Shell(display);
Label labelSatu = new Label(shell, SWT.LEFT); labelSatu.setText("Contoh Label Satu"); labelSatu.setBounds(20,15, 100, 20); labelSatu.setForeground(new Color(display, 255, 255, 255)); // putih labelSatu.setBackground(new Color(display, 12,119,190)); // biru
Label labelDua = new Label(shell, SWT.LEFT); labelDua.setText("Contoh Label Dua"); labelDua.setBounds(20,55, 100, 20); //menggunakan angka hexadesimal labelDua.setBackground(new Color(display, Integer.parseInt("ED",16), Integer.parseInt("F3",16), Integer.parseInt("75",16))); // kuning muda labelDua.setForeground(new Color(display, 0, 0, 0)); // hitam shell.setText("Contoh Warna !"); shell.setSize(300, 200); shell.setBackground(new Color(display, new RGB(255,255,255))); shell.open();
while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
public static void main(String[] args) { new WarnaWidget().tampil(); } } |
Hasil Eksekusi
~~ Selesai ~~
|
|