Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
html
Menciptakan JSlider tanpa argument, maka akan terjadi horizontal slider dengan de ault data model. Model mempunyai initial !alue "#, minimum #, ma$imum %##, dan e$tent #.
JSlider &roperties
Setelah menciptakan JSlider, anda dapat memodi ikasi data dalam model. Anda dapat memperoleh model dengan method public 'ounded(angeModel getModel)*, dan kemudian memodi ikasi secara langsung. Anda juga dapat memanggil secara langsung method dari komponen ini. Memperoleh nilai )!alue*: method get+alue)*,
Strin, #al$e = Strin,.#al$e 0(/lider.,etVal$e()); label./etTe4t(#al$e);
Properti JSlider
// Li/tener/ &$blic #oid addC.an,eLi/tener(C.an,eLi/tener li/tener); &$blic #oid re%o#eC.an,eLi/tener(C.an,eLi/tener li/tener); // t.er 6et.od/ &$blic #oid /etRan,e:ro&ertie/(int #al$e* int e4tent* int %ini%$%* int %a4i%$%* boolean ad9$/tin,);
.he ollo4ing picture sho4s an application that uses a slider to control animation speed:
Try this: 1. Click the Launch button to run SliderDemo using Java Web Start (download JDK or later!. "lternativel#$ to com%ile and run the e&am%le #oursel'$ consult the e&am%le inde&. (. )se the slider to ad*ust the animation s%eed. +. ,ush the slider to - to sto% the animation.
'elo4 is the code rom the Slider2e%o.9a#a ile that creates the slider in the pre!ious e$ample.
/tatic 0inal int @:SA6I" = '; /tatic 0inal int @:SA6AB = 3'; /tatic 0inal int @:SAI"IT = )8; //initial 0ra%e/ &er /econd . . . JSlider 0ra%e/:erSecond = new JSlider(JSlider.H RI! "TAL* @:SA6I"* @:SA6AB* @:SAI"IT); 0ra%e/:erSecond.addC.an,eLi/tener(t.i/); //T$rn on label/ at %a9or tic; %ar;/. 0ra%e/:erSecond./et6a9orTic;S&acin,()'); 0ra%e/:erSecond./et6inorTic;S&acin,()); 0ra%e/:erSecond./et:aintTic;/(tr$e); 0ra%e/:erSecond./et:aintLabel/(tr$e);
'y de ault, spacing or major and minor tick marks is zero. .o see tick marks, you must e$plicitly set the spacing or either major or minor tick marks )or both* to a non7zero !alue and call the /et:aintTic;/(tr$e) method. 1o4e!er, you also need labels or your tick marks. .o display standard, numeric labels at major tick mark locations, set the major tick spacing, then call the set&aint0abels)true* method. .he e$ample program pro!ides labels or its slider in this 4ay. 'ut you are not constrained to using only these labels. 6ustomizing 0abels on a Slider sho4s you ho4 to customize slider labels. 8n addition, a ne4 slider eature a!ailable in J5K 6 allo4s you to set a ont or the JSlider component.
@ont 0ont = new @ont(CSeri0C* @ont.ITALIC* )8); 0ra%e/:erSecond./et@ont(0ont);
9hen you mo!e the slider:s knob, the state6hanged method o the slider:s 6hange0istener is called. ;or in ormation about change listeners, re er to 1o4 to 9rite a 6hange 0istener. 1ere is the change listener code that reacts to slider !alue changes:
&$blic #oid /tateC.an,ed(C.an,eE#ent e) > JSlider /o$rce = (JSlider)e.,etSo$rce(); i0 (D/o$rce.,etVal$eI/Ad9$/tin,()) > int 0&/ = (int)/o$rce.,etVal$e(); i0 (0&/ == ') > i0 (D0rozen) /to&Ani%ation(); ? el/e > dela1 = )''' / 0&/; ti%er./et2ela1(dela1); ti%er./etInitial2ela1(dela1 E )'); i0 (0rozen) /tartAni%ation(); ? ? ?
<otice that the state6hanged method changes the animation speed only i the get+alue8sAdjusting method returns alse. Many change e!ents are ired
as the user mo!es the slider knob. .his program is interested only in the inal result o the user:s action.
.he source 'or this %rogram can be 'ound in Slider2e%o-.9a#a. Click the Launch button to run SliderDemo( using Java Web Start (download JDK or later!. "lternativel#$ to com%ile and run the e&am%le #oursel'$ consult the e&am%le inde&.
.he 'ollowing code creates the slider and customi/es its labels0
//Create t.e /lider JSlider 0ra%e/:erSecond = new JSlider(JSlider.VERTICAL* @:SA6I"* @:SA6AB* @:SAI"IT); 0ra%e/:erSecond.addC.an,eLi/tener(t.i/); 0ra%e/:erSecond./et6a9orTic;S&acin,()'); 0ra%e/:erSecond./et:aintTic;/(tr$e); //Create t.e label table Ha/.table labelTable = new Ha/.table(); labelTable.&$t( new Inte,er( ' )* new JLabel(CSto&C) ); labelTable.&$t( new Inte,er( @:SA6AB/)' )* new JLabel(CSlowC) ); labelTable.&$t( new Inte,er( @:SA6AB )* new JLabel(C@a/tC) ); 0ra%e/:erSecond./etLabelTable( labelTable ); 0ra%e/:erSecond./et:aintLabel/(tr$e);
1ach ke#2value %air in the hashtable s%eci'ied with the /etLabelTable method gives the %osition and the value o' one label. .he hashtable ke# must be o' an Inte,er t#%e and must have a value within the slider3s range at which to %lace the label. .he hashtable value associated with each ke# must be a Co%&onent ob*ect. .his demo uses JLabel instances with te&t onl#. "n interesting modi'ication would be to use JLabel instances with icons or buttons that move the knob to the label3s %osition. )se the createStandardLabel/ method o' the JSlider class to create a set o' numeric labels %ositioned at a s%eci'ic interval. 4ou can also modi'# the table returned b# the createStandardLabel/ method in order to customi/e it.
Creating the Slider 6ine .uning the Slider3s "%%earance Watching the Slider 7%erate Working Directl# with the Data 8odel Creating the Slider Constructor "urpose Creates a hori/ontal slider with the range - to 1-and an initial value o' 9-. Creates a hori/ontal slider with the s%eci'ied minimum and ma&imum values. .he third int argument$ when %resent$ s%eci'ies the slider3s initial value. Creates a slider with the s%eci'ied orientation$ which must be either JSlider.H RI! "TAL or JSlider.VERTICAL. .he last three int arguments$ when %resent$ s%eci'# the slider3s minimum$ ma&imum$ and initial values$ res%ectivel#. Creates a hori/ontal slider with the s%eci'ied model$ which manages the slider3s minimum$ ma&imum$ and current values and their relationshi%s. "urpose Sets or gets the slider3s current value. .he set method also %ositions the slider3s knob. Sets or gets the orientation o' the slider. ,ossible values
JSlider(! JSlider(int min$ int ma&! JSlider(int min$ int ma&$ int value! JSlider(int orientation! JSlider(int orientation$ int min$ int ma&$ int value!
JSlider(:ounded;ange8odel!
6ine .uning the Slider3s "%%earance $ethod void set<alue(int! int get<alue(! void set7rientation(int!
int get7rientation(! void set5nverted(boolean! boolean get5nverted(! void set8inimum(int! int get8inimum(! void set8a&imum(int! int get8a&imum(! void set8a*or.ickS%acing(int! int get8a*or.ickS%acing(! void set8inor.ickS%acing(int! int get8inor.ickS%acing(!
are JSlider.H RI! "TAL or JSlider.VERTICAL. Sets or gets whether the ma&imum is shown at the le't o' a hori/ontal slider or at the bottom o' a vertical one$ thereb# inverting the slider3s range. Sets or gets the minimum or ma&imum values o' the slider. .ogether$ these methods set or get the slider3s range.
Sets or gets the range between ma*or and minor ticks. 4ou must call /et:aintTic;/(tr$e) 'or the tick marks to a%%ear.
void set,aint.icks(boolean! Sets or gets whether tick marks are %ainted on the slider. boolean get,aint.icks(! Sets or gets whether labels are %ainted on the slider. void set,aintLabels(boolean! 4ou can %rovide custom labels with /etLabelTable or boolean get,aintLabels(! get automatic labels b# setting the ma*or tick s%acing to a non2/ero value. void Sets or gets the labels 'or the slider. 4ou must call setLabel.able(Dictionar#! /et:aintLabel/(tr$e) 'or the labels to a%%ear. Dictionar# getLabel.able(! =ashtable createStandardLabels(int! =ashtable createStandardLabels(int$ int! set6ont(*ava.awt.6ont! $ethod void addChangeListener(ChangeListener! boolean get<alue5s"d*usting(! Creates a standard set o' numeric labels. .he 'irst int argument s%eci'ies the increment$ the second int argument s%eci'ies the starting %oint. When le't uns%eci'ied$ the starting %oint is set to the slider3s minimum number. Sets the 'ont 'or slider labels . Watching the Slider 7%erate "urpose ;egisters a change listener with the slider. Determines whether the user gesture to move the slider3s knob is com%lete. "urpose .he inter'ace re>uired 'or the slider3s data model. "n im%lementation o' the 5o$ndedRan,e6odel inter'ace. Sets or gets the data model used b# the slider. 4ou can also set the model b# using the constructor that takes a
Working Directl# with the Data 8odel Class% #nter&ace% or $ethod :ounded;ange8odel De'ault:ounded;ange8odel void set8odel(! get8odel(!
(in JSlider)