Jscrollbar has two common constructor methods.
1. jscrollbar1 = new jscrollbar ();
Create a scroll bar with the specified direction and the following initial values:
Minimum = 0
Maximum= 100
Value = 0
Extent = 10
2. jscrollbar2 = new jscrollbar (INT orientation, int value, int extent, int min, int max );
The parameters are as follows:
1 is horizontal or vertical, which can be:
Jscrollbar. Horizontal or: jscrollbar. Vertical
2 Initial Value
3. Expand the size of the scroll bar to reach the max-extent. For example, if the maximum value is 100 and the extend value is 10, the scroll bar can reach the maximum value of 90.
4 min
5 max
Below is
The first is the default constructor, and the second is the constructor.
Jscrollbar3 = new jscrollbar (jscrollbar. Horizontal ,);
You can see that the scroll bar of the default constructor can only reach 90
An important method is: jscrollbar1.getvalue ()
Set the maximum and minimum values:
{
Jscrollbar1.setminimum (int I );
Jscrollbar1.setmaximum (int I );
}
The internal class implements adjustmentlistener to monitor the value changes of the jscrollbar;
- Jscrollbar1.addadjustmentlistener (New adjustmentlistener ()
- {
- Public void adjustmentvaluechanged (adjustmentevent EVT ){
- Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar1.getvalue ()));
- }
- });
Below is the source code, you can try to change it
- Import java. AWT. event .*;
- Import javax. Swing .*;
- Public class newjframes extends javax. Swing. jframe {
- Private Static final long serialversionuid = 1l;
- Private jtextfield jtextfield1;
- Private jlabel jlabel2;
- Private jbutton jbutton1;
- Private jscrollbar jscrollbar3;
- Private jlabel jlabel1;
- Private jtextfield jtextfield3;
- Private jtextfield jtextfield2;
- Private jscrollbar jscrollbar1;
- Public static void main (string [] ARGs ){
- Swingutilities. invokelater (New runnable ()
- {
- Public void run (){
- Newjframes inst = new newjframes ();
- Inst. setlocationrelativeto (null );
- Inst. setvisible (true );
- }
- });
- }
- Public newjframes (){
- Super ();
- Initgui ();
- }
- Private void initgui (){
- Try {
- Setdefaclocloseoperation (windowconstants. dispose_on_close );
- Getcontentpane (). setlayout (null );
- Settitle ("scroll bar test ");
- {
- Jscrollbar3 = new jscrollbar (jscrollbar. Horizontal );
- Getcontentpane (). Add (jscrollbar3 );
- Jscrollbar3.setbounds (12,220,358, 34 );
- Jscrollbar3.addadjustmentlistener (New adjustmentlistener ()
- {
- Public void adjustmentvaluechanged (adjustmentevent EVT ){
- Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar3.getvalue ()));
- }
- });
- }
- {
- Jscrollbar1 = new jscrollbar ();
- Getcontentpane (). Add (jscrollbar1 );
- Jscrollbar1.setbounds );
- Jscrollbar1.setorientation (swingconstants. Horizontal );
- Jscrollbar1.addadjustmentlistener (New adjustmentlistener ()
- {
- Public void adjustmentvaluechanged (adjustmentevent EVT ){
- Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar1.getvalue ()));
- }
- });
- }
- {
- Jtextfield1 = new jtextfield ();
- Getcontentpane (). Add (jtextfield1 );
- Jtextfield1.setbounds (242,5, 138,26 );
- }
- {
- Jtextfield2 = new jtextfield ();
- Getcontentpane (). Add (jtextfield2 );
- Jtextfield2.setbounds );
- }
- {
- Jtextfield3 = new jtextfield ();
- Getcontentpane (). Add (jtextfield3 );
- Jtextfield3.setbounds );
- }
- {
- Jlabel1 = new jlabel ();
- Getcontentpane (). Add (jlabel1 );
- Jlabel1.settext ("set minimum value ");
- Jlabel1.setbounds );
- }
- {
- Jlabel2 = new jlabel ();
- Getcontentpane (). Add (jlabel2 );
- Jlabel2.settext ("set maximum value ");
- Jlabel2.setbounds (12, 78, 80, 25 );
- }
- {
- Jbutton1 = new jbutton ();
- Getcontentpane (). Add (jbutton1 );
- Jbutton1.settext ("set ");
- Jbutton1.setbounds (123,128, 22 );
- Jbutton1.addactionlistener (New actionlistener ()
- {
- Public void actionreceivmed (actionevent EVT ){
- If (jtextfield2.gettext (). isempty () | jtextfield3.gettext (). isempty ()){
- Jscrollbar1.setvalue (jscrollbar1.getvalue () + 1 );
- } Else {
- Jscrollbar1.setminimum (integer. valueof (jtextfield2.gettext ()));
- Jscrollbar1.setmaximum (integer. valueof (jtextfield3.gettext ()));
- }
- }
- });
- }
- Pack ();
- Setsize (400,300 );
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }