Wednesday, 17 July 2013

how to use a scrollbar in java

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements AdjustmentListener
{
 Scrollbar sbr,sbg,sbb;
 Panel p;

 MyFrame()
 {
  setTitle("Scrollbar Demo");
  setSize(400,300);
  setBackground(new Color(200,200,255));

  sbr=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);
  sbg=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);
  sbb=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);

  p=new Panel();
  p.setBackground(Color.black);

  setLayout(new GridLayout(1,2,20,20));
 
  Panel p1=new Panel();
  p1.setLayout(new GridLayout(1,4,15,5));
  p1.add(sbr);
  p1.add(sbg);
  p1.add(sbb);
  p1.add(new Label());

  add(p1);
  add(p);
  
  sbr.addAdjustmentListener(this);
  sbg.addAdjustmentListener(this);
  sbb.addAdjustmentListener(this);

  addWindowListener(new MyWndAdapter(this)); 
 }

 public Insets getInsets()
 {
  return new Insets(50,20,20,20);
 }

 public void adjustmentValueChanged(AdjustmentEvent ae)
 {
  int r,g,b;
  r=255-sbr.getValue();
  g=255-sbg.getValue();
  b=255-sbb.getValue();

  p.setBackground(new Color(r,g,b));
 }
}


class MyWndAdapter extends WindowAdapter
{
 MyFrame mf;

 MyWndAdapter(MyFrame mf)
 {
  this.mf=mf;
 }

 public void windowClosing(WindowEvent we)
 {
  mf.dispose();
  System.exit(0);
 }
}

class SBDemo
{
 public static void main(String arg[])
 {
  MyFrame mf=new MyFrame();
  mf.setVisible(true);
 }
}
---------------------------------------------------------------------------------------------------------------

steps to run the Demo program of Scrollbar
step1: save the file as frm.java file in the bin and refresh it.

step2: after that open ur command prompt and write the path of ur jdk
       
       like my jdk path is
       C:\java\jdk1.7.0_10\bin
step3: if u not know how to make path then follow the steps below





step4: after that to execute the program do the following

C:\java\jdk1.7.0_10\bin>javac SBDemo.java and press enter

step5: if the step4 is successfull then perform this step

C:\java\jdk1.7.0_10\bin>java SBDemo



and enjoy!!  ;)

if u have any problem then msg me at
rawlot007bhati@gmail.com

No comments:

Post a Comment