?? sdatoolsbar.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package cn.sda.ui;import cn.sda.event.KeybordEvent;import cn.sda.event.PointerEvent;import cn.sda.event.ToolsBarClickEvent;import cn.sda.event.ToolsBarOnChangeEvent;import java.util.Vector;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Font;/** * * @author Administrator */public class SDAToolsBar extends SDABaseControl { private byte buttonAlignType = SDAConsts.ktHorizontal; private int borderColor = SDAConsts.clBlack; //選中按鈕顏色 private int focusButtonBackColor = SDAConsts.clWhite; private int focusButtonForeColor = SDAConsts.clBlack; //是否顯示標(biāo)題 private boolean showCaption = false; //是否顯示按鈕的邊框 private boolean showButtonRect = true; //是否按照標(biāo)題自動伸縮按鈕 private boolean autoSizeButton = true; //組件列表 private Vector controlList = null; //當(dāng)前位置 private SDAToolButton curButton = null; //事件 private ToolsBarClickEvent onClickEvent = null; private ToolsBarOnChangeEvent onChangeEvent = null; //背景圖 private Image backImage = null; private boolean stretchImage = true; //按鈕圖片排布方式 private int glyphAlignType = SDAConsts.blGlyphTop; //按鈕的缺省尺寸 private int buttonSize = 40; public SDAToolsBar() { super(); setWidth(120); setHeight(22); setDock(SDAConsts.dsTop); controlList = new Vector(); setOnKeyDown(new KeybordEvent() { public void Event(SDABaseControl ctrl, int keyCode) { //鍵盤 doKeydown(keyCode); } }); setOnPointerPressed(new PointerEvent() { public void Event(SDABaseControl ctrl, int x, int y) { //點 doPointerPress(x, y); } }); } public void paint() { if (!IsCanPaint()) { return; } Graphics g = form.getGraphics(); SetClip(g); g.setFont(getFont()); int thisWidth = getWidth(); int thisHeight = getHeight(); //框 if (!transparent) { g.setColor(backColor); fillRect(g, 0, 0, thisWidth, thisHeight); } g.setColor(borderColor); drawRect(g, 0, 0, thisWidth, thisHeight); //畫子組件 PaintChilds(); //畫按鈕和分割線 paintButton(g); //焦點 if (isFoucsed()) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } PaintChilds(); } //計算設(shè)置按鈕的寬度 private void setButtonSize() { SDAToolButton button = null; int buttonHeight = getHeight() - 4; int buttonWidth = getWidth() - 4; Font ft = getFont(); int fontheight = ft.getHeight(); int num = 0; int buttonPos = 2; for (int i = 0; i < controlList.size(); i++) { if (controlList.elementAt(i) instanceof SDAToolButton) { button = (SDAToolButton) controlList.elementAt(i); if (autoSizeButton) { if (buttonAlignType == SDAConsts.ktHorizontal) { button.setHeight(buttonHeight); button.setTop(2); button.setLeft(buttonPos); if (button.getImage() != null && button.getCaption().length() > 0 && (glyphAlignType == SDAConsts.blGlyphLeft || glyphAlignType == SDAConsts.blGlyphRight)) { button.setWidth(ft.stringWidth(button.getCaption() + "xx") + button.getImage().getWidth()); } else { num = ft.stringWidth(button.getCaption() + "xx"); if (button.getImage() != null) { if (num < button.getImage().getWidth()) { num = button.getImage().getWidth(); } } button.setWidth(num); } buttonPos += button.getWidth() + 2; } else { button.setLeft(2); button.setTop(buttonPos); button.setWidth(buttonWidth); if (button.getImage() != null && button.getCaption().length() > 0 && (glyphAlignType == SDAConsts.blGlyphTop || glyphAlignType == SDAConsts.blGlyphBottom)) { button.setHeight(fontheight + 4 + button.getImage().getHeight()); } else { num = fontheight + 2; if (button.getImage() != null) { if (num < button.getImage().getHeight() + 2) { num = button.getImage().getHeight() + 2; } } button.setHeight(num); } buttonPos += button.getHeight() + 2; } } else { if (buttonAlignType == SDAConsts.ktHorizontal) { button.setTop(2); button.setLeft(buttonPos); button.setHeight(buttonHeight); button.setWidth(buttonSize); buttonPos += button.getWidth() + 2; } else { button.setLeft(2); button.setTop(buttonPos); button.setWidth(buttonWidth); button.setHeight(buttonSize); buttonPos += button.getHeight() + 2; } } } else { if (buttonAlignType == SDAConsts.ktHorizontal) { buttonPos += ((SDAToolSeperator) controlList.elementAt(i)).getWidth(); } else { buttonPos += ((SDAToolSeperator) controlList.elementAt(i)).getHeight(); } } } } //畫按鈕和分割 private void paintButton(Graphics g) { int thisWidth = getWidth(); int thisHeight = getHeight(); Font ft = getFont(); int fontHeight = ft.getHeight(); //設(shè)置大小 setButtonSize(); //畫 int buttonPos = 2; SDAToolButton button = null; SDAToolSeperator sp = null; Image image = null; String caption = ""; //畫背景 if (backImage != null) { if (stretchImage) { image = SDAImageUtils.processImage(backImage, thisWidth, thisHeight, SDAImageUtils.MODE_STRETCH); } else { image = backImage; } drawImage(g, image, 0, 0, 0); } if (buttonAlignType == SDAConsts.ktHorizontal) { for (int i = 0; i < controlList.size(); i++) { if (controlList.elementAt(i) instanceof SDAToolButton) { button = (SDAToolButton) controlList.elementAt(i); caption = button.getCaption(); if (showButtonRect) { g.setColor(borderColor); drawRect(g, buttonPos, 2, button.getWidth(), button.getHeight()); } SetClip(g, buttonPos, 2, button.getWidth(), button.getHeight()); image = button.getImage(); //焦點按鈕 if (button.equals(curButton)) { g.setColor(focusButtonBackColor); fillRect(g, buttonPos + 1, 3, button.getWidth() - 1, button.getHeight() - 1); g.setColor(focusButtonForeColor); } if (image != null) { if (showCaption && caption.length() > 0) { if (glyphAlignType == SDAConsts.blGlyphTop) { drawImage(g, image, buttonPos + (button.getWidth() - image.getWidth()) / 2 + 1, (button.getHeight() - image.getHeight() - fontHeight) / 2 + 3, 0); drawString(g, caption, buttonPos + (button.getWidth() - ft.stringWidth(caption)) / 2 + 1, (button.getHeight() - image.getHeight() - fontHeight) / 2 + image.getHeight() + 3); } if (glyphAlignType == SDAConsts.blGlyphBottom) { drawImage(g, image, buttonPos + (button.getWidth() - image.getWidth()) / 2 + 1, (button.getHeight() - image.getHeight() - fontHeight) / 2 + fontHeight + 3, 0); drawString(g, caption, buttonPos + (button.getWidth() - ft.stringWidth(caption)) / 2 + 1, (button.getHeight() - image.getHeight() - fontHeight) / 2 + 3); } if (glyphAlignType == SDAConsts.blGlyphLeft) { drawImage(g, image, buttonPos + (button.getWidth() - image.getWidth() - ft.stringWidth(caption) - 2) / 2 + 1, (button.getHeight() - image.getHeight()) / 2 + 3, 0); drawString(g, caption, buttonPos + (button.getWidth() - ft.stringWidth(caption) - image.getWidth() - 2) / 2 + image.getWidth() + 3, (button.getHeight() - fontHeight) / 2 + 3); } if (glyphAlignType == SDAConsts.blGlyphRight) { drawImage(g, image, buttonPos + (button.getWidth() - image.getWidth() - ft.stringWidth(caption) - 2) / 2 + ft.stringWidth(caption) + 3, (button.getHeight() - image.getHeight()) / 2 + 3, 0); drawString(g, caption, buttonPos + (button.getWidth() - ft.stringWidth(caption) - image.getWidth() - 2) / 2 + 1, (button.getHeight() - fontHeight) / 2 + 3); } } else { drawImage(g, image, buttonPos + (button.getWidth() - image.getWidth()) / 2 + 1, (button.getHeight() - image.getHeight()) / 2 + 3, 0); } } else { if (showCaption) { g.setColor(foreColor); drawString(g, button.getCaption(), buttonPos + (button.getWidth() - ft.stringWidth(button.getCaption())) / 2 + 1, (button.getHeight() - fontHeight) / 2 + 3); } } SetClip(g); buttonPos += button.getWidth() + 2; } else { sp = (SDAToolSeperator) controlList.elementAt(i); if (!showButtonRect) { g.setColor(borderColor); drawLine(g, buttonPos + sp.getWidth() / 2 - 1, 4, buttonPos + sp.getWidth() / 2 - 1, thisHeight - 4); g.setColor(0x00ffffff); drawLine(g, buttonPos + sp.getWidth() / 2, 4, buttonPos + sp.getWidth() / 2, thisHeight - 4); } buttonPos += sp.getWidth(); } } } if (buttonAlignType == SDAConsts.ktVertical) { for (int i = 0; i < controlList.size(); i++) { if (controlList.elementAt(i) instanceof SDAToolButton) { button = (SDAToolButton) controlList.elementAt(i); caption = button.getCaption(); if (showButtonRect) { g.setColor(borderColor); drawRect(g, 2, buttonPos, button.getWidth(), button.getHeight()); } SetClip(g, 2, buttonPos, button.getWidth(), button.getHeight()); image = button.getImage(); //焦點按鈕 if (button.equals(curButton)) { g.setColor(focusButtonBackColor); fillRect(g, 3, buttonPos + 1, button.getWidth() - 1, button.getHeight() - 1); g.setColor(focusButtonForeColor); } if (image != null) { if (showCaption && caption.length() > 0) { if (glyphAlignType == SDAConsts.blGlyphTop) { drawImage(g, image, (button.getWidth() - image.getWidth()) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight() - fontHeight) / 2 + 1, 0); drawString(g, caption, (button.getWidth() - ft.stringWidth(caption)) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight() - fontHeight) / 2 + image.getHeight() + 1); } if (glyphAlignType == SDAConsts.blGlyphBottom) { drawImage(g, image, (button.getWidth() - image.getWidth()) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight() - fontHeight) / 2 + fontHeight + 1, 0); drawString(g, caption, (button.getWidth() - ft.stringWidth(caption)) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight() - fontHeight) / 2 + 1); } if (glyphAlignType == SDAConsts.blGlyphLeft) { drawImage(g, image, (button.getWidth() - image.getWidth() - ft.stringWidth(caption) - 2) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight()) / 2 + 1, 0); drawString(g, caption, (button.getWidth() - ft.stringWidth(caption) - image.getWidth() - 2) / 2 + image.getWidth() + 5, buttonPos + (button.getHeight() - fontHeight) / 2 + 1); } if (glyphAlignType == SDAConsts.blGlyphRight) { drawImage(g, image, (button.getWidth() - image.getWidth() - ft.stringWidth(caption) - 2) / 2 + ft.stringWidth(caption) + 5, buttonPos + (button.getHeight() - image.getHeight()) / 2 + 1, 0); drawString(g, caption, (button.getWidth() - ft.stringWidth(caption) - image.getWidth() - 2) / 2 + 3, buttonPos + (button.getHeight() - fontHeight) / 2 + 1); } } else { drawImage(g, image, (button.getWidth() - image.getWidth()) / 2 + 3, buttonPos + (button.getHeight() - image.getHeight()) / 2 + 1, 0); } } else { if (showCaption) { g.setColor(foreColor); //打字 drawString(g, String.valueOf(caption), (button.getWidth() - ft.stringWidth(caption)) / 2 + 3, buttonPos + 2); } } SetClip(g); buttonPos += button.getHeight() + 2; } else { sp = (SDAToolSeperator) controlList.elementAt(i); if (!showButtonRect) { g.setColor(borderColor); drawLine(g, 4, buttonPos + sp.getHeight() / 2 - 1, thisWidth - 4, buttonPos + sp.getHeight() / 2 - 1); g.setColor(0x00ffffff); drawLine(g, 4, buttonPos + sp.getHeight() / 2, thisWidth - 4, buttonPos + sp.getHeight() / 2); } buttonPos += sp.getHeight(); } } } } //增加button public void addButton(SDAToolButton button) { if (!controlList.contains(button)) { controlList.addElement(button); }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -