Tips for using jscrollbar

Source: Internet
Author: User

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;

  1. Jscrollbar1.addadjustmentlistener (New adjustmentlistener ()
  2. {
  3. Public void adjustmentvaluechanged (adjustmentevent EVT ){
  4. Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar1.getvalue ()));
  5. }
  6. });

Below is the source code, you can try to change it

  1. Import java. AWT. event .*;
  2. Import javax. Swing .*;
  3. Public class newjframes extends javax. Swing. jframe {
  4. Private Static final long serialversionuid = 1l;
  5. Private jtextfield jtextfield1;
  6. Private jlabel jlabel2;
  7. Private jbutton jbutton1;
  8. Private jscrollbar jscrollbar3;
  9. Private jlabel jlabel1;
  10. Private jtextfield jtextfield3;
  11. Private jtextfield jtextfield2;
  12. Private jscrollbar jscrollbar1;
  13. Public static void main (string [] ARGs ){
  14. Swingutilities. invokelater (New runnable ()
  15. {
  16. Public void run (){
  17. Newjframes inst = new newjframes ();
  18. Inst. setlocationrelativeto (null );
  19. Inst. setvisible (true );
  20. }
  21. });
  22. }
  23. Public newjframes (){
  24. Super ();
  25. Initgui ();
  26. }
  27. Private void initgui (){
  28. Try {
  29. Setdefaclocloseoperation (windowconstants. dispose_on_close );
  30. Getcontentpane (). setlayout (null );
  31. Settitle ("scroll bar test ");
  32. {
  33. Jscrollbar3 = new jscrollbar (jscrollbar. Horizontal );
  34. Getcontentpane (). Add (jscrollbar3 );
  35. Jscrollbar3.setbounds (12,220,358, 34 );
  36. Jscrollbar3.addadjustmentlistener (New adjustmentlistener ()
  37. {
  38. Public void adjustmentvaluechanged (adjustmentevent EVT ){
  39. Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar3.getvalue ()));
  40. }
  41. });
  42. }
  43. {
  44. Jscrollbar1 = new jscrollbar ();
  45. Getcontentpane (). Add (jscrollbar1 );
  46. Jscrollbar1.setbounds );
  47. Jscrollbar1.setorientation (swingconstants. Horizontal );
  48. Jscrollbar1.addadjustmentlistener (New adjustmentlistener ()
  49. {
  50. Public void adjustmentvaluechanged (adjustmentevent EVT ){
  51. Jtextfield1.settext (string. valueof ("the current value of the scroll bar is:" + jscrollbar1.getvalue ()));
  52. }
  53. });
  54. }
  55. {
  56. Jtextfield1 = new jtextfield ();
  57. Getcontentpane (). Add (jtextfield1 );
  58. Jtextfield1.setbounds (242,5, 138,26 );
  59. }
  60. {
  61. Jtextfield2 = new jtextfield ();
  62. Getcontentpane (). Add (jtextfield2 );
  63. Jtextfield2.setbounds );
  64. }
  65. {
  66. Jtextfield3 = new jtextfield ();
  67. Getcontentpane (). Add (jtextfield3 );
  68. Jtextfield3.setbounds );
  69. }
  70. {
  71. Jlabel1 = new jlabel ();
  72. Getcontentpane (). Add (jlabel1 );
  73. Jlabel1.settext ("set minimum value ");
  74. Jlabel1.setbounds );
  75. }
  76. {
  77. Jlabel2 = new jlabel ();
  78. Getcontentpane (). Add (jlabel2 );
  79. Jlabel2.settext ("set maximum value ");
  80. Jlabel2.setbounds (12, 78, 80, 25 );
  81. }
  82. {
  83. Jbutton1 = new jbutton ();
  84. Getcontentpane (). Add (jbutton1 );
  85. Jbutton1.settext ("set ");
  86. Jbutton1.setbounds (123,128, 22 );
  87. Jbutton1.addactionlistener (New actionlistener ()
  88. {
  89. Public void actionreceivmed (actionevent EVT ){
  90. If (jtextfield2.gettext (). isempty () | jtextfield3.gettext (). isempty ()){
  91. Jscrollbar1.setvalue (jscrollbar1.getvalue () + 1 );
  92. } Else {
  93. Jscrollbar1.setminimum (integer. valueof (jtextfield2.gettext ()));
  94. Jscrollbar1.setmaximum (integer. valueof (jtextfield3.gettext ()));
  95. }
  96. }
  97. });
  98. }
  99. Pack ();
  100. Setsize (400,300 );
  101. } Catch (exception e ){
  102. E. printstacktrace ();
  103. }
  104. }
  105. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.