---
title: "J2ME实现的滚动条ScrollBar"
author: "alexlikeask"
date: "2010-02-03T14:47:16Z"
tags: []
categories: ["移动开发"]
slug: "j2me-scrollbar"
draft: false
lastmod: "2010-02-03T15:07:47Z"
id: 586731
url: "https://alexlikeask.iteye.com/blog/586731"
---

&lt;object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="248" height="331" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt; &lt;param name="src" value="/admin/blogs/586731/e:/scrollbar.png"&gt; &lt;embed type="application/x-shockwave-flash" width="248" height="331" src="/admin/blogs/586731/e:/scrollbar.png"&gt;&lt;/embed&gt;&lt;/object&gt; 只需要修改其中的count 变量，就可以调整整个菜单中元素的数量。

 

只要大家修改一下，就可以封装成一个滚动条ScroolBar的控件，以后使用的话，无非就是往这个屏幕里添加控件而已，很简单。

 

大家多提宝贵意见啊！

 

看了要回帖啊@@@@@@@

 

 

 

 

public class TestCanvas extends Canvas implements Runnable{  
   
 private boolean isColorChange = false,isRun = true,isVibrate = false;  
 private int dragBox\_Width = 17,dragBox\_Height ;  
 private int dragBox\_X,dragBox\_Y;  
 private int dragLine\_Width = 9,dragLine\_Height;  
 public int ScreenWidth,ScreenHeight;  
 private Thread th = null;  
 private int onDragColor = 0x40e0d0,defaultDragColor = 0x696969;  
 private int curKey;  
 private int vibrateTime = 20;  
 private int vendor,offset = 0,bufImage\_Y;  
 private Image bufImage = null;  
 private Graphics bufGraphics = null;  
 int count = 64;  
   
 TestCanvas(){  
  setFullScreenMode(true);  
  ScreenWidth = this.getWidth();  
  ScreenHeight = this.getHeight();  
  bufImage = Image.createImage(ScreenWidth - dragBox\_Width - 5 - (dragBox\_Width - dragLine\_Width)/2,count\*30);  
  bufGraphics = bufImage.getGraphics();  
    
  bufImage\_Y = 0;  
  dragLine\_Height = ScreenHeight - 40;  
  double f = (bufImage.getHeight() /dragLine\_Height);  
  dragBox\_Height = (int)(dragLine\_Height/f);  
  dragBox\_X = ScreenWidth - dragBox\_Width - 5 - (dragBox\_Width - dragLine\_Width)/2;  
  dragBox\_Y = (ScreenHeight - dragLine\_Height)/2;  
  start();  
 }  
   
 public void paint(Graphics g){  
  g.setColor(0);  
  g.fillRect(0, 0, ScreenWidth, ScreenHeight);  
  g.setColor(0xc0c0c0);  
  g.fillRoundRect(ScreenWidth - dragBox\_Width - 5,  
    (ScreenHeight - dragLine\_Height)/2, dragLine\_Width, dragLine\_Height, 5, 5);  
  g.setColor(isColorChange?onDragColor:defaultDragColor);  
  g.fillRoundRect(dragBox\_X, dragBox\_Y, dragBox\_Width, dragBox\_Height, 9, 9);  
  bufGraphics.setColor(0);  
  bufGraphics.fillRect(0,0,bufImage.getWidth(), bufImage.getHeight());  
  bufGraphics.setColor(0xffffff);  
  for(int i=0;i&lt;count;i++){  
   bufGraphics.drawString((i+1)+"", 20, i\*30, 0);  
  }  
  g.drawImage(bufImage, 0, offset, 0);  
 }  
   
 public void run(){  
  while(isRun){  
   try{  
    repaint();  
    serviceRepaints();  
    sleep(100L);  
   }catch(Exception e){  
      
   }  
  }  
 }  
   
 public void start(){  
  if(th == null){  
   th = new Thread(this);  
  }  
  th.start();  
 }  
   
 public static void sleep(long time){  
  try{  
   Thread.sleep(time);  
  }catch(Exception e){  
     
  }  
 }  
   
 public void keyPressed(int key){  
  curKey = key;  
  switch(curKey){  
  case -7:  
   MidletTest.instance.exitApp();  
   break;  
  }  
 }  
   
 public void keyReleased(int key){  
  curKey = 999;  
 }  
   
// public void pointerPressed(int x, int y) {  
//  isColorChange = true;  
//  if(isVibrate == false){  
//   MidletTest.instance.vibrate(vibrateTime);  
//  }    
//  isVibrate = true;  
//  vendor = dragBox\_Y;  
//  dragBox\_Y  = y - dragBox\_Height/2;   
//  dragBox\_Y = (dragBox\_Y - (ScreenHeight - dragLine\_Height) / 2) &lt;= 0 ? (ScreenHeight - dragLine\_Height) / 2 - 1  
//    : dragBox\_Y &gt;= ScreenHeight - (ScreenHeight - dragLine\_Height)  
//      / 2 - dragBox\_Height ? ScreenHeight  
//      - (ScreenHeight - dragLine\_Height) / 2 - dragBox\_Height  
//      : y - dragBox\_Height/2;  
//  int i = (dragBox\_Y - vendor)\*(640/200);  
//  System.out.println("屏幕需要走的:"+i);  
//  if(i&gt;0){  
//   offset -= i;  
//  }else{  
//   offset += i;  
//  }  
// }

 public void pointerReleased(int x, int y) {  
  isColorChange = false;   
  isVibrate = false;  
 }

 public void pointerDragged(int x, int y) {  
  isColorChange = true;  
  if(isVibrate == false){  
//   MidletTest.instance.vibrate(vibrateTime);  
  }    
  isVibrate = true;  
  vendor = dragBox\_Y;  
  dragBox\_Y  = y - dragBox\_Height/2;   
  dragBox\_Y = (dragBox\_Y - (ScreenHeight - dragLine\_Height) / 2) &lt;= 0 ? (ScreenHeight - dragLine\_Height) / 2 - 1  
    : dragBox\_Y &gt;= ScreenHeight - (ScreenHeight - dragLine\_Height)  
      / 2 - dragBox\_Height ? ScreenHeight  
      - (ScreenHeight - dragLine\_Height) / 2 - dragBox\_Height  
      : y - dragBox\_Height/2;  
      bufImage.getHeight();  
  float f1 = (bufImage.getHeight() - ScreenHeight);  
  float f2 = ScreenHeight - 40 - dragBox\_Height;  
  float f = (f1/f2);  
  float i = ((dragBox\_Y - vendor)\*f);  
  if(y&lt;((ScreenHeight - dragLine\_Height)/2+(dragBox\_Height/2))){  
   offset = 0;  
  }  
  else  
   if((y&gt;=((ScreenHeight - dragLine\_Height)/2+dragBox\_Height/2))&&(y&lt;=(ScreenHeight -(ScreenHeight - dragLine\_Height)/2-dragBox\_Height/2) )){  
   offset -= i;  
  }  
  else if((y&gt;(ScreenHeight -(ScreenHeight - dragLine\_Height)/2-dragBox\_Height/2))){  
   int w = 0;  
   w = -((count\*30)-ScreenHeight);  
   offset = w;  
  }  
    
 }

}