Sei sulla pagina 1di 4

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

html

Label
Dengan kelas JLabel, kita dapat menampilkan teks dan image yang tidak bisa terpilih. Catatan bahwa label secara default not opaque tidak buram!. "ika anda ingin mewarnai background label, set properti opaque menjadi #true# diberi tanda cek!. $ecara kode untuk menset opaque adalah: label.setOpaque(true); Contoh, buat project, dengan sebuah frame %abel&rame!. $truktur project:

Dari gambar: terdapat image: middle.gif pada package belajar.images &rame berisi ' buah label label(, label'! )asukkan kedalam frame: *omp. $wing "%abel "%abel $eting +ariabel: label( ,rop te-t:.eks pada label +ariabel: label' ,roperti: te-t: .eks dengan image icon: middle.gif hori/ontal.e-t,osition: C01.02 vertical.e-t,osition: 34..4) Desain frame:

$truktur project 1et3eans:

*omponen "%abel "%abel

,roperti variable te-t variable te-t icon

1ilai label( .eks pada label label' .eks dengan image midle.gif

Label API Setting or Getting the Label's Contents Method or Constructor Purpose Creates a "%abel instance, initiali/ing it to have the specified te-t/image/alignment. .he int argument specifies the hori/ontal alignment of the label5s contents within its drawing area. .he hori/ontal alignment must be one of the following constants defined in the $wingConstants interface which "%abel implements!: %0&., C01.02, 2678., %09D617, or .296%617. &or ease of locali/ation, we strongly recommend using %09D617 and .296%617, rather

JLabel(Icon) JLabel(Icon, int) JLabel(String) JLabel(String, Icon, int) JLabel(String, int) JLabel()

than %0&. and 2678.. void setText(String) String getText() void setIcon(Icon) Icon getIcon() $ets or gets the te-t displayed by the label. :ou can use 8.)% tags to format the te-t, as described in ;sing 8.)% in $wing Components. $ets or gets the image displayed by the label. $ets or gets the letter that should look like a keyboard alternative. .his is helpful when a label describes a component such as a te-t field! that has a keyboard alternative but cannot display it. 6f the label&or property is also set using set%abel&or!, then when the user activates the mnemonic, the keyboard focus is transferred to the component specified by the label&or property.

void setDispla ed!ne"onic(c#ar) c#ar getDispla ed!ne"onic()

$ets or gets a hint as to which character in the te-t should be decorated to represent the mnemonic. .his is useful when you have two instances of the void same character and wish to decorate the setDispla ed!ne"onicIndex(int) second instance. &or e-ample, int setDisplayed)nemonic6nde<! getDispla ed!ne"onicIndex() decorates the character that is at position < that is, the =th character in the te-t!. 1ot all types of look and feel may support this feature. void setDisabledIcon(Icon) Icon getDisabledIcon() $ets or gets the image displayed by the label when it is disabled. 6f you do not specify a disabled image, then the look and feel creates one by manipulating the default image. Purpose

Fine Tuning the Label's Appearance Method $ets or gets the area on the label where its contents should be placed. .he void $wingConstants interface defines five set8ori/ontal9lignment int! possible values for hori/ontal alignment: void set+ertical9lignment int! %0&., C01.02 the default for image>only int get8ori/ontal9lignment ! labels!, 2678., %09D617 the default for int get+ertical9lignment ! te-t>only labels!, .296%617. &or vertical alignment: .4,, C01.02 the default!, and 34..4). void $ets or gets the location where the label5s

te-t will be placed, relative to the label5s set8ori/ontal.e-t,osition int! image. .he $wingConstants interface void defines five possible values for hori/ontal set+ertical.e-t,osition int! position: %09D617, %0&., C01.02, 2678., int get8ori/ontal.e-t,osition ! and .296%617 the default!. &or vertical int get+ertical.e-t,osition ! position: .4,, C01.02 the default!, and 34..4). void set6con.e-t7ap int! int get6con.e-t7ap ! $ets or gets the number of pi-els between the label5s te-t and its image.

Potrebbero piacerti anche