⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textboard.java

📁 J2me实现的MSN Messeger客户端程序。聊天、添加好友、删除好友、阻止好友
💻 JAVA
字号:
/**
 *字符串布局类
 *@CopyRight:eshouji.com
 *@Author:ceze
 *@Version 1.0 2004/4/22
 */


package vitular.ui;

import javax.microedition.lcdui.*;


public class TextBoard
    extends MsgCtrlBase {

  private static  final int BK_RGB = 0X4A92FF;
  private static final int FONT_RGB = 0XFFFF99;
  private int lineGap;
  private String text;//文本内容
  private String title;//标题
  private int largeFontHeight;

  private int totalLine;//文本显示所需行数
  private int lineStarIndex[]; //换行位置
  private int drawStarLine; //显示开始行
  private int maxLine;//一屏可显示的最大行数
  private boolean isScroll = false; //是否需要滚屏
  private int backColor; //背景颜色 默认黑色
  private int frontColor; //前景颜色 默认白色

  public TextBoard(MsgCanvas canvas)
  {
    super(canvas);
  }


  /**
   * 构造函数 行间隔1
   * @param Text String
   * @param Title String
   */
  public TextBoard(MsgCanvas canvas, String Text, String Title){
    this(canvas, Text, Title, 1);
  }

  /**
   * 构造函数 用默认颜色全屏显示
   * @param Text String 文本内容字符串
   * @param Title String 标题
   * @param Gap int 行间隙
   */
  public TextBoard(MsgCanvas canvas, String Text, String Title, int Gap){
    this(canvas, Text, Title, Gap, BK_RGB, FONT_RGB);
  }

  /**
   * 构造函数 默认颜色指定区域显示
   * @param Text String
   * @param Title String
   * @param Gap int 行间隙
   * @param Left int
   * @param Top int
   * @param Width int
   * @param Height int
   */
  public TextBoard(MsgCanvas canvas, String Text, String Title, int Gap, int Left, int Top, int Width, int Height)
  {
    this(canvas, Text, Title, Gap, BK_RGB, FONT_RGB, Left, Top, Width, Height);
  }

  /**
   * 构造函数 全屏显示
   * @param Text String
   * @param Title String
   * @param Gap int 行间隙
   * @param bColor int
   * @param fColor int
   */
  public TextBoard(MsgCanvas canvas, String Text,String Title,int Gap , int bColor, int fColor){
    super(canvas);//, Text, Title, Gap, bColor, fColor, 0, 0);
    init( Text, Title, Gap, bColor, fColor);

  }

  /**
   *构造函数 指定区域大小和文字颜色<br>
   *Text:文本内容字符串<br>
   *Titel: 标题字符串<br>
   *Gap:各行之间的空隙
   * bColor 背景颜色
   * fColor 前景颜色
   *Left:布局顶点X<br>
   *Left:布局顶点Y<br>
   *Width:布局宽度<br>
   *Height:布局高度<br>
   */
  public TextBoard(MsgCanvas canvas, String Text,String Title,int Gap , int bColor, int fColor,int Left, int Top, int Width,
                      int Height) {
    super(canvas, Left, Top, Width, Height);
    init( Text, Title, Gap, bColor, fColor);

  }

  /**
   * 一些初始化的工作
   * @param Text String
   * @param Title String
   * @param Gap int
   * @param bColor int
   * @param fColor int
   */
  private void init(String Text,String Title,int Gap , int bColor, int fColor){
    largeFontHeight = textFont.getHeight();
    text = Text;
    title = Title;

  /*  boardX = Left;
    boardY = Top;
    boardWidth = Width;
    boardHeight = Height;
    */
   lineGap = Gap;



  //计算行数及换行位置
  totalLine = 0;

  if(text != null){
    lineStarIndex = new int[text.length()];
    lineStarIndex[0] = 0;
    int begin = 0;
    for (int i = 0; i < text.length(); i++) {
      char ch = text.charAt(i);
      if (Util.getStrWidth(text.substring(begin, i), Font.SIZE_MEDIUM) >= (width - 10) ||
          i == text.length() - 1 || ch == 0x24) { //显示区域两边各留10
        totalLine++;
        //ch == 0x24 字符$强制换行.
        if (ch == 0x24){//强制换行或文字结束
          begin = i + 1;
          lineStarIndex[totalLine] = begin; // i + 1;
        }
        else if( i == text.length() - 1){
          lineStarIndex[totalLine] = i + 1;
          lineStarIndex[totalLine + 1] = text.length();
        }
        else{
          begin = i;
          lineStarIndex[totalLine] = begin - 1;
        }
        }
    }//~end for
  }//~end if

    //是否需要滚屏
    if(title == null){
      maxLine = (height - 6) / (largeFontHeight + lineGap);
    }
    else{
      maxLine = (height - largeFontHeight - 8)/(largeFontHeight + lineGap);
    }
    if(maxLine < totalLine)
        isScroll = true;

    if(isScroll)
      setPosition(left + 2, top);
    else
      setPosition(left + 4, top);

    drawStarLine = 0;
    backColor = bColor;
    frontColor = fColor;

  }

  private void scrollUp(){

    drawStarLine -= 2;
    if(drawStarLine < 0)
      drawStarLine = 0;
  }

  private void scrollDown(){
    if(drawStarLine <= totalLine - maxLine)
      drawStarLine += 2;
  }

  public void keyPressed(int keyCode){
    switch(keyCode){
      case MsgCanvas.KEY_UP:
      case MsgCanvas.UP:
        scrollUp();
        break;
      case MsgCanvas.KEY_DOWN:
      case MsgCanvas.DOWN:
        scrollDown();
        break;
      case MsgCanvas.KEY_SOFT2:
      case MsgCanvas.KEY_CLEAR:
        dispatchEvent(EVT_TEXTBOARD_CLOSE, -1, title);
        title = null;
        text = null;
        System.gc();
        break;
    }
  }

  /**
   * 画出文本内容
   * @param g Graphics
    */
  public void updateSelf(Graphics g) {
    if(visible == false || g == null)
      return;


    g.setClip(left, top, width, height);
    g.setFont(textFont);

    if(backColor != -1){
      //填充背景
      g.setColor(backColor);
      g.fillRect(left, top, width, height);
      if(title != null){
        g.setColor(0x50);
        g.fillRect(left, top, width, largeFontHeight + 2);
        g.setColor(0xDDDDDD);
        g.drawLine(left, largeFontHeight + 2, width, largeFontHeight + 2);
      }
    }
    else{
      //画边框
      g.setColor(0x505050);
      g.drawRect(left, top, width, height);
      g.setColor(0x20);
      g.drawRect(left, top, width -1 , height -1);
    }

    int pY = posY + 1;
    //标题
    if(title!=null){
      g.setColor(0xEEEEEE);
      g.drawString(title, 1 + (width - left)>>1, 1 + pY, g.HCENTER | g.TOP);
      g.setColor(frontColor);
      g.drawString(title, (width-left)>>1, pY, g.HCENTER | g.TOP);
      pY += largeFontHeight + 2;
    }
    //内容
    if(text == null)
      return;

  //  Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
  //  g.setFont(font);
    pY += 1;
    g.setColor(frontColor);
    String s = "";
    int drawLine = 0;
    int endLine  = 0;
    drawLine = totalLine >= maxLine ? maxLine : totalLine;
    for (int lineIndex = drawStarLine, line = 0; lineIndex < drawStarLine + drawLine; line++,lineIndex++) {
      if (lineIndex > totalLine)
        break;
      int endIndex;

      if (text.charAt(lineStarIndex[lineIndex + 1] - 1) == 0x24) { //遇到强制换行符
        endIndex = lineStarIndex[lineIndex + 1] - 1;
      }
      else {
        endIndex = lineStarIndex[lineIndex + 1];
      }

      s = text.substring(lineStarIndex[lineIndex], endIndex);
      if(s != null || s != "")
        g.drawString(s, posX, pY + line * (largeFontHeight + lineGap),
                     g.TOP | g.LEFT);
        s = null;
    }


    //滚动条
 /*   if(isScroll){
      g.setColor(0x404040);
      g.drawRect(width - 4, pY, 4, height - pY - 1);
      int barY = pY + (drawStarLine * (height - pY - 6) * maxLine) / (totalLine - maxLine);
      if(barY > height - 6)
        barY = height - 6;
      g.setColor(0x707070);
      g.fillRect(width - 3, barY, 2, 5);
    }
 */
    System.gc();
  }

  /**
   * 取得文本板上的文字
   * @return String
   */
  public String getText(){
    return text;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -