The Test Slider Applet
/*
Example applet that shows the use of a Scrollbar (slider) in a
simple user interface.
The slider is used to input integer value to the program. The current
value of the slider is recorded whenever the user interacts with the
slider and the value is displayed.
For any GUI component, one has to do the following:
create the GUI object (you will do this in init()
add the object to the applet's display area
register the applet as a listener for it
write the listener method (and declare that the applet implements it
*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class testSlider extends Applet implements AdjustmentListener
{
Scrollbar slider; // will hold the GUI object
int sliderValue; // value of the slider is recorded here
public void init() {
// create the GUI object
slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100);
// add it to the applet
add(slider);
// register this applet as a listener for the object
slider.addAdjustmentListener(this);
} // end of init
public void paint( Graphics g ) {
// display the present value of slider
g.drawString( "Value = " + sliderValue, 20, 100 );
} // end of paint
public void adjustmentValueChanged(AdjustmentEvent e) {
// record the value of the slider
sliderValue = slider.getValue();
repaint();
} // end of adjustmentValueChanged
} // end of testSlider applet