Sei sulla pagina 1di 3

http://docs.oracle.com/javase/tutorial/uiswing/components/button.

html#radiobuttonapi

How to Use Radio Buttons


Radio button digunakan sebagai implementasi user interface pilihan. User dapat memilih salah satu dari sejumlah pilihan yang tersedia. Setiap satu item pilihan diimplementasikan dengan sebuah radio button. Komponen Swing mendukung radio button dengan kelas JRadioButton dan ButtonGroup. engelompokan radio button dikerjakan dengan ButtonGroup Karena JRadioButton diturunkan dari AbstractButton! Swing radio button mempunyai seluruh karakteristik button.

Contoh Aplikasi:
Buat project dengan sebuah frame "RadioButton#emo$ Struktur project%

#ari gambar ini! package components berisi J&rame bernama RadioButton.ja'a. package components.images berisi lima buah gambar "Bird.gif! (at.gif! #og.gif! ig.gif! Rabbit.gif$. #esain form%

Seting komponen% )o 1 2 3 Komponen JFrame ButtonGroup roperti title variable )ilai RadioButtonDemo bg1 panel1

JPanel variable (untuk diisi bua! JRadioButton JRadioButton variable (masukkan ke te$t JPanel# selected icon JRadioButton variable (masukkan ke te$t JPanel#

"

rbBird Bird dicentang (true# Bird%gi& rb'at 'at rbDog Dog rbRabbit Rabbit rbPig Pig gambar './0.R Bird%gi& 1dikosongkan2

JRadioButton variable (masukkan ke te$t JPanel# JRadioButton variable (masukkan ke te$t JPanel# JRadioButton variable (masukkan ke te$t JPanel# J,abel variable !ori-ontalAlignment icon te$t

Konstruktor dari kelas RadioButton ditambah kode berikut. "cetak tebal$.


public RadioButton(# 1 init'omponents(#3 bg1%add(rbBird#3 bg1%add(rb'at#3 bg1%add(rbDog#3 bg1%add(rbRabbit#3 bg1%add(rbPig#3 2

*'ent% action erformed pada rbBird%


private void rbBirdActionPer&ormed(4ava%a5t%event%Action.vent evt# 1 66 07D7 add 8our !andling code !ere9 gambar%set:con(ne5 4ava$%s5ing%:mage:con(get'lass(#%getResource( ;6components6images6Bird%gi&;###3 2

untuk radio button lainnya juga diberi e'ent yang sama seperti rbBird! hanya file gif yang diset "set+con$ disesuaikan.

Radio Button Constructors Constructor JRadioButton(Action JRadioButton(!tring JRadioButton(!tring" boolean JRadioButton(#con JRadioButton(#con" boolean JRadioButton(!tring" #con JRadioButton(!tring" #con" boolean JRadioButton( JRadioButton+enu#tem(Action JRadioButton+enu#tem(!tring JRadioButton+enu#tem(#con JRadioButton+enu#tem(!tring" #con JRadioButton+enu#tem( Purpose Create a JRadioButton instance. $he string argument speci%ies the te&t" i% an'" that the radio button should displa'. !imilarl'" the :con argument speci%ies the image that should be used instead o% the loo( and %eel)s de%ault radio button image. !peci%'ing the boolean argument as true initiali*es the radio button to be selected" subject to the approval o% the ButtonGroup object. #% the boolean argument is absent or &alse" then the radio button is initiall' unselected. Create a JRadioButton<enu:tem instance. $he arguments are interpreted in the same wa' as the arguments to the JRadioButton constructors" e&cept that an' speci%ied icon is shown in addition to the normal radio button icon.

Potrebbero piacerti anche