Constructors of Scrollbar:
-Scrollbar()
creates a new vertical scrolling bar.
-Scrollbar(int orientation)
creates a new scrolling bar with specified orientation. The orientation can be Scrollbar.HORIZONTAL or Scrollbar.VERTICAL.
-Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
creates a new scrolling bar with specified orientation, initial value, visible amount, minimum and maximum values.
Useful methods of the Scrollbar class:
-getMaximum()
reads the maximum value of the scrolling bar.
-getMinimum()
reads the minimum value of the scrolling bar.
-getValue()
reads the value of the scrolling bar.
-setBlockIncrement(int v)
sets the block increment of the scrolling bar.
-setMaximum(int v)
sets the maximum value of the scrolling bar.
-setMinimum(int v)
sets the minimum value of the scrolling bar.
-setUnitIncrement(int v)
sets the unit increment of the scrolling bar.
-setValue(int v)
sets the value of the scrolling bar.
Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class InterfaceShow extends Frame implements AdjustmentListener{
InterfaceShow(String title){
setTitle(title);
setSize(new Dimension(500,300));
setVisible(true);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Scrollbar colorscroll= new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
colorscroll.setUnitIncrement(5);
colorscroll.setBlockIncrement(15);
colorscroll.addAdjustmentListener(this);
add(colorscroll, BorderLayout.NORTH);
}
public void adjustmentValueChanged(AdjustmentEvent e){
Scrollbar scr=(Scrollbar)e.getSource();
setBackground(new Color(scr.getValue()));
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using ScrollBar");
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class InterfaceShow extends Frame implements AdjustmentListener{
InterfaceShow(String title){
setTitle(title);
setSize(new Dimension(500,300));
setVisible(true);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Scrollbar colorscroll= new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 255);
colorscroll.setUnitIncrement(5);
colorscroll.setBlockIncrement(15);
colorscroll.addAdjustmentListener(this);
add(colorscroll, BorderLayout.NORTH);
}
public void adjustmentValueChanged(AdjustmentEvent e){
Scrollbar scr=(Scrollbar)e.getSource();
setBackground(new Color(scr.getValue()));
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using ScrollBar");
}
}
No comments:
Post a Comment