Saturday, April 30, 2011

JColorChooser di java

Contoh program java berikut ini mendemokan bagaimana penggunaan class JColorChooser untuk menampilkan pilihan warna. Dalam program dicontohkan saat ditekan tombol “Ubah Warna” maka akan ditampilkan window warna dan jika dipilih salah satu warna, maka background frame akan berubah sesuai dengan warna yang dipilih.
Berikut ini tampilannya:
  source code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ShowColors2 extends JFrame {

               private JButton tblUbah;

               private Color warna = Color.LIGHT_GRAY;

               private Container container;

               //atur tampilan GUI

               public ShowColors2() {

                               super ("Menggunakan JColorChooser");                          

                               container = getContentPane();

                               container.setLayout (new FlowLayout());

                               //atur event pd tombol tblUbah

                               tblUbah = new JButton ("Ubah Warna");

                               tblUbah.addActionListener (

                                              new ActionListener() {

                    public void actionPerformed (ActionEvent event) {

             warna = JColorChooser.showDialog ( ShowColors2.this, "Pilih Warna", warna);
                                  if (warna == null)

                           warna = Color.LIGHT_GRAY;

                                                          container.setBackground (warna);
                                                             }
                                              } //end of inner class
                               ); //end of method addActionListener

                               container.add (tblUbah);

                               setSize (400,300);

                               setVisible (true);

               } //end of constructor

               public static void main (String args[]) {

                ShowColors2 test = new ShowColors2();

        test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
               }
}

0 comments:

Post a Comment