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

📄 sdatoolsbar.java

📁 很好的UI界面源码..还有自己的输入法,可以更换风格.可以学习和使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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;    //是否显示标题    private boolean showCaption = false;    //是否显示按钮的边框    private boolean showButtonRect = true;    //是否按照标题自动伸缩按钮    private boolean autoSizeButton = true;    //组件列表    private Vector controlList = null;    //当前位置    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();    }    //计算设置按钮的宽度    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();        //设置大小        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);        }

⌨️ 快捷键说明

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