Anda di halaman 1dari 5

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

html

How to Use the Common Button API


Sebuah JButton umumnya merupakan tombol yang, ketika diklik oleh pengguna, melakukan tindakan tertentu. JButton memiliki ActionListener yang dilekatkan padanya, yang pada dasarnya mendefinisikan tugas yang harus dilakukan ketika tombol diklik. Pada umumnya JButton membuat action listener individu untuk setiap tombol. Pola ini biasanya terlihat sebagai berikut:
JButton button1 = new JButton("Click me!"); button1.addActionListener(new ActionListener() { ublic !oid action"er#ormed(Action$!ent e!t) { %% called w&en button clicked %% button1Action"er#ormed(e!t); ' '); ri!ate !oid t#Action"er#ormed((a!a.awt.e!ent.Action$!ent e!t) { %% )*+* add ,our &andlin- code &ere. . . . '

Berikut aplikasi yang menampilkan

button:

Langkah: Buat pro!ect dengan sebuah frame "Button#emo$% Struktur pro!ect:

#ari gambar ini, package components berisi J&rame bernama Button#emo.!ava. package components.images berisi tiga buah gambar "left.gif, middle.gif, right.gif%. Seting komponen: )o 1 / *omponen Properti J1rame JButton title !ariable te2t icon &ori3ontal)e2t"osition mnemonic tool)i )e2t )ilai +emo Button b1 +isable button ten-a& ri-&t.-i# L$1) 4+5 Klik tombol ini utk mendisable tombol tengah !ariable b/ te2t Button )en-a& icon middle.-i# &ori3ontal)e2t"osition C$6)$7 !ertical)e2t"osition B*))*8 mnemonic 4B5 tool)i )e2t Tombol ini tidak mengerjakan sesuatu !ariable b0 te2t $nable button ten-a& icon le#t.-i# &ori3ontal)e2t"osition 7:;<) enabled {&ilan-kan centan-' mnemonic 4$5 tool)i )e2t Klik tombol ini utk mengenable tombol tengah

JButton

JButton

'vent: actionPerformed pada b(: ri!ate !oid b1Action"er#ormed((a!a.awt.e!ent.Action$!ent e!t) { %% )*+* add ,our &andlin- code &ere. b/.set$nabled(#alse); b1.set$nabled(#alse); b0.set$nabled(true); ' 'vent: actionPerformed pada b : ri!ate !oid b0Action"er#ormed((a!a.awt.e!ent.Action$!ent e!t) { %% )*+* add ,our &andlin- code &ere. b/.set$nabled(true); b1.set$nabled(true); b0.set$nabled(#alse); '

Button API
Setting or +etting the Button,s -ontents Method or Constructor
JButton(Action) JButton(=trin-> :con) JButton(=trin-) JButton(:con) JButton() !oid setAction(Action) Action -etAction() !oid set)e2t(=trin-) =trin- -et)e2t() !oid set:con(:con) :con -et:con() !oid set+isabled:con(:con) :con -et+isabled:con() !oid set"ressed:con(:con) :con -et"ressed:con() !oid set=elected:con(:con) :con -et=elected:con() !oid set+isabled=elected:con(:con) :con -et+isabled=elected:con() set7ollo!er$nabled(boolean) boolean is7ollo!er$nabled() !oid set7ollo!er:con(:con) :con -et7ollo!er:con() !oid set7ollo!er=elected:con(:con) :con -et7ollo!er=elected:con()

Purpose
-reate a JButton instance, initiali.ing it to have the specified te/t0image0action. Set or get the button,s properties according to values from the Action instance. Set or get the te/t displayed by the button. 1ou can use 234L formatting, as described in 5sing 234L in S6ing -omponents. Set or get the image displayed by the button 6hen the button isn,t selected or pressed. Set or get the image displayed by the button 6hen it is disabled. 7f you do not specify a disabled image, then the look and feel creates one by manipulating the default image. Set or get the image displayed by the button 6hen it is being pressed. Set or get the image displayed by the button 6hen it is selected. 7f you do not specify a disabled selected image, then the look and feel creates one by manipulating the selected image. 5se set7ollo!er:con(some:con) to make the button display the specified icon 6hen the cursor passes over it. 3he set7ollo!er=elected:con method lets you specify the rollover icon 6hen the button is selected 8 this is useful for t6o9state buttons such as toggle buttons. Setting the rollover icon automatically calls set7ollo!er(true), enabling rollover.

&ine 3uning the Button,s Appearance Method or Constructor Purpose


Set or get 6here in the button its contents should be placed. 3he AbstractButton class !oid allo6s any one of the follo6ing values for set<ori3ontalAli-nment(int) !oid set?erticalAli-nment(int) hori.ontal alignment: 7:;<), L$1), C$6)$7 int -et<ori3ontalAli-nment() "the default%, L$A+:6;, and )7A:L:6;. &or int -et?erticalAli-nment() vertical alignment: )*", C$6)$7 "the default%, and B*))*8. !oid Set or get 6here the button,s te/t should be set<ori3ontal)e2t"osition(int) placed, relative to the button,s image. 3he

AbstractButton class allo6s any one of the !oid follo6ing values for hori.ontal position: set?ertical)e2t"osition(int) L$1), C$6)$7, 7:;<), L$A+:6;, and int -et<ori3ontal)e2t"osition() int -et?ertical)e2t"osition() )7A:L:6; "the default%. &or vertical position: )*", C$6)$7 "the default%, and B*))*8. !oid set8ar-in(:nsets) :nsets -et8ar-in() !oid set1ocus"ainted(boolean) boolean is1ocus"ainted() Set or get the number of pi/els bet6een the button,s border and its contents. Set or get 6hether the button should look different 6hen it has the focus.

!oid setBorder"ainted(boolean) Set or get 6hether the border of the button boolean isBorder"ainted() should be painted. !oid set:con)e2t;a (int) int -et:con)e2t;a () Set or get the amount of space bet6een the te/t and the icon displayed in this button.

7mplementing the Button,s &unctionality Method or Constructor Purpose


Set or get the keyboard alternative to clicking the button. :ne form of the set8nemonic method accepts a character argument; ho6ever, the S6ing team recommends that you use an int argument instead, specifying a @e,$!ent.?@AX constant. Set or get a hint as to 6hich character in the te/t should be decorated to represent the mnemonic. )ote that not all look and feels may support this. Set or get the name of the action performed by the button.

!oid set8nemonic(int) c&ar -et8nemonic()

!oid set+is la,ed8nemonic:nde2(int) int -et+is la,ed8nemonic:nde2() !oid setActionCommand(=trin-) =trin- -etActionCommand()

!oid addActionListener(ActionListener) Add or remove an ob!ect that listens for ActionListener action events fired by the button. remo!eActionListener() !oid add:temListener(:temListener) Add or remove an ob!ect that listens for :temListener remo!e:temListener() item events fired by the button. !oid set=elected(boolean) boolean is=elected() Set or get 6hether the button is selected. 4akes sense only for buttons that have on0off state, such as check bo/es. Programmatically perform a <click<. 3he optional argument specifies the amount of time "in milliseconds% that the button should look pressed.

!oid doClick() !oid doClick(int)

Set or get the amount of time "in !oid set8ultiClick)&res&&old(lon-) milliseconds% re=uired bet6een mouse lon- -et8ultiClick)&res&&old() press events for the button to generate corresponding action events.