ComboBox.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- /**
- *
- * @author swaroop_kumar
- */
- public class ComboBox extends UIControl{
- GTantra font;
- String label;
- int charHeight;
- ListControl listControl;
- boolean disablePopUp = false;
- Image image;
- private static final int padding = 3;
- private int pallet = 0;
- public ComboBox(Image image,String text,GTantra font,String list[],int currentIndex) {
- this.image = image;
- this.font = font;
- this.label = text;
- charHeight = Util.getMaxCharHeight(font,text);
- int max = getMaxWidth(list);
- if(max < font.getStringWidth(text))
- max = font.getStringWidth(text);
- setWidth(max + (padding * 2) + image.getWidth());
- setHeight(image.getHeight());
- listControl = new ListControl(this,font, width, currentIndex, list);
- }
- int getMaxWidth(String arry[])
- {
- int max= 0;
- for (int idx = 0; idx < arry.length; idx++) {
- if(font.getStringWidth(arry[idx]) > max)
- max = font.getStringWidth(arry[idx]);
- }
- return max;
- }
- public boolean isDisablePopUp() {
- return disablePopUp;
- }
- public void setDisablePopUp(boolean disablePopUp) {
- this.disablePopUp = disablePopUp;
- }
- public String getText()
- {
- return this.label ;
- }
- public void setText(String str)
- {
- this.label = str;
- }
- public void paint(Graphics g) {
- g.setColor(0xFFFFFF);
- g.fillRect(0, 0, width - 1, getHeight() - 1);
- g.drawImage(image, getWidth() - image.getWidth(), 0, 0);
- g.setColor(0x7F9DB5);
- g.drawRect(0, 0, width - 1, getHeight() - 1);
- font.setCurrentPallete(pallet);
- font.drawString(g, label,((width - image.getWidth()) >> 1) + 1, (getHeight() - charHeight) >> 1, GTantra.TEXT_HCENTER);
- int transY = g.getTranslateY();
- if(listControl.isVisible())
- {
- int clipX, clipY , clipWidth,clipHeight;
- clipX = g.getClipX();
- clipY = g.getClipY();
- clipWidth = g.getClipWidth();
- clipHeight = g.getClipHeight();
- listControl.setHeight(font.getFontHeight() * 4);
- if(transY + getHeight() + (font.getFontHeight() * 4) >= DisplayManager.getInst().getHeight() /*- CommonMethods.getSoftkeyBarHeight()*/)
- {
- listControl.setPoistion(0, -listControl.getHeight());
- }
- else
- {
- listControl.setPoistion(0, getHeight());
- }
- g.setClip(0, listControl.getY(), listControl.getWidth()+5, listControl.getHeight());
- listControl.paintUI(g);
- g.setClip(clipX, clipY, clipWidth, clipHeight);
- }
- }
- public boolean keyPressed(int keycode) {
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- if (!disablePopUp && gameKey == DisplayManager.FIRE || keycode == KeyEvent.KEY_SOFT_CENTER || keycode == DisplayManager.KEY_NUM5) {
- listControl.setVisible(true);
- DisplayManager.getInst().setListControl(listControl);
- return true;
- }
- return false;
- }
- }