Home » » Contoh Program JColorChooser di Java

Contoh Program JColorChooser di Java

Posted by KEPOIN IT on Sunday, November 6, 2011


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:
















Berikut ini program lengkapnya:

01
import java.awt.*;
02
import java.awt.event.*;

03
import javax.swing.*;
04


05
public class ShowColors2 extends JFrame {
06


07
    private JButton tblUbah;
08
    private Color warna = Color.LIGHT_GRAY;

09
    private Container container;
10


11
    //atur tampilan GUI
12
    public ShowColors2() {

13
        super ("Menggunakan JColorChooser");       
14


15
        container = getContentPane();
16
        container.setLayout (new FlowLayout());

17

18
        //atur event pd tombol tblUbah

19
        tblUbah = new JButton ("Ubah Warna");
20


21
        tblUbah.addActionListener (
22
            new ActionListener() {

23
                public void actionPerformed (ActionEvent event) {
24
                    warna = JColorChooser.showDialog ( ShowColors2.this, "Pilih Warna", warna);

25

26
                    if (warna == null)

27
                        warna = Color.LIGHT_GRAY;
28
                    container.setBackground (warna);

29
                }
30
            } //end of inner class

31
        ); //end of method addActionListener
32


33
        container.add (tblUbah);
34
        setSize (400,300);

35
        setVisible (true);
36
    } //end of constructor

37

38
    public static void main (String args[]) {

39
        ShowColors2 test = new ShowColors2();
40
        test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

41
    }
42
}

Thanks for reading & sharing KEPOIN IT

Previous
« Prev Post

0 comments:

Post a Comment

Cari Artikel

Paling Dilihat