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

📄 select.java

📁 手机程序
💻 JAVA
字号:
package com.gameislive.browser.element;

import javax.microedition.lcdui.Graphics;
import java.util.Vector;
import com.gameislive.browser.Browser;
import com.gameislive.browser.Tools;

public class Select extends Element implements FormElement{

	public Vector labels = new Vector();
	
	String name;
	
	Vector values = new Vector();
	
	Vector selected;
	
	String showLabel;
	
	int size;
	
	int maxLength = 100;
	
	Browser browser;
	
	int curSelect;
	
	public Select(int hyperlinksId,int lineId,byte type,Browser browser){
		// 文本框肯定可以选择,所以第一个参数为true
		super(true,hyperlinksId,lineId,type);
		this.browser = browser;

		if(type == SELECT_MULTIPLE){
			selected = new Vector();
		}
		height = browser.fontHeight;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}
	
	public void addOption(String value,String label,boolean selected){
		values.addElement(value);
		labels.addElement(label);
		
		int w = browser.strWidth(label);
		if(w > width){
			if(w + 24 > maxLength){
				width = maxLength;
			}else{
				width = w + 24;
			}
		}
		
		if(type == SELECT_MULTIPLE){
			this.selected.addElement(new Boolean(selected));
			if(selected){
				curSelect = this.selected.size()-1;
				
				showLabel = (String)labels.elementAt(curSelect);
				if(browser.strWidth(showLabel) > maxLength){
					showLabel = Tools.CutString(showLabel, maxLength);
				}		
			}
		}else{
			
			if(values.size()==1) setValue((values.size()-1)+"");
			
			if(selected){
				setValue((values.size()-1)+"");
			}
		}

		//#if DEBUG
		//# System.out.println("addOption: "+value+" ==> "+label + " show:"+showLabel);
		//#endif
	}
	
	/**
	 * 更改某选项的选中状态
	 * @param index
	 */
	public void changeSelected(int index){
		if(type == SELECT_MULTIPLE){
			if(selected.size()>index && index>=0){
				boolean b = ((Boolean)selected.elementAt(index)).booleanValue();
				b = !b;
				selected.setElementAt(new Boolean(b), index);	
			}
		}
	}
	
	public boolean getSelectedState(int index){
		if(index>=0 && index<selected.size()){
			return ((Boolean)selected.elementAt(index)).booleanValue();
		}
		return false;
	}
	
	public void showSelected(){
		if(type == SELECT_MULTIPLE){
			for(int i=0;i<selected.size();i++){
				boolean b = ((Boolean)selected.elementAt(i)).booleanValue();
				if(b){
					curSelect = i;
					showLabel = (String)labels.elementAt(curSelect);
					if(browser.strWidth(showLabel) > maxLength){
						showLabel = Tools.CutString(showLabel, maxLength);
					}
					return;
				}
			}
			curSelect = 0;
			showLabel = null;
		}
	}
	
	public void setValue(String value){
		if(type == SELECT_MULTIPLE){
			
		}else{
			curSelect = Integer.parseInt(value);
			showLabel = (String)labels.elementAt(curSelect);
			if(browser.strWidth(showLabel) > maxLength){
				showLabel = Tools.CutString(showLabel, maxLength);
			}
		}
	}
	
	public String getValue(){
		if(values.size()==0) return null;
		if(type == SELECT_MULTIPLE){
			String string = null;
			for(int i=0;i<selected.size();i++){
				if(((Boolean)selected.elementAt(i)).booleanValue()){
					if(string==null){
						string = (String)values.elementAt(i);
					}else{
						string += "," + (String)values.elementAt(i);
					}
				}
			}
			return string;
		}else{
			return (String) values.elementAt(curSelect);
		}
	}
	
	public void setMaxLength(int maxLength){
		this.maxLength = maxLength;
	}
	
	public int getMaxLength(){
		return maxLength;
	}
	
	public int getWidth(){
		return width;
	}
	
	public int getHeight(){
		return height + 2;
	}
	
	public void draw(int y,int hyperlinks,Graphics g){		
		
		if(hyperlinksId == hyperlinks){
    		g.setColor(0x0000ff);
        	g.fillRect(x,y,width,height);
        	g.setColor(0xffffff);
		}else{
    		g.setColor(0xffffff);
        	g.fillRect(x,y,width,height);
       		g.setColor(0x000000);
		}

   		g.drawString("\u25BC",x+width-1,y,Graphics.TOP|Graphics.RIGHT);
    	g.drawRect(x,y,width,height);
    	
        if(showLabel!=null){
        	g.drawString(showLabel, x + 7, y, 20);
        }
	}		
}

⌨️ 快捷键说明

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