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

📄 label.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 JAVA
字号:
package prototype;

import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Label extends Component {
	public static final int CENTRE = 0;
	public static final int RIGHT = 1;
	public static final int LEFT = 2;
	private int gintBeginIndex = 0;
	private int gintLineHeight = 15;
	private final int defaultBorderColor = 0xFFEEEE;
	private boolean border = false;
	private String text;
	private Font font;
	private int fontColor;
	private int textAlign = -1;
	Vector vector = null;
//	int x;
//	int y;
	private Image image;

	public int getTextAlign() {
		return textAlign;
	}

	public void setTextAlign(int textAlign) {
		this.textAlign = textAlign;
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getFontColor() {
		return fontColor;
	}

	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}

	public boolean isBorder() {
		return border;
	}

	public void setBorder(boolean border) {
		this.border = border;
	}

	public Label(int x, int y, int width, int height) {
		super(x, y, width, height);
	}

	protected void paintImpl(Graphics g) {
		// TODO paint the Label's image or text
		int alignOffset = 0;
		if ( border) {
			g.setColor(defaultBorderColor);
			g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
		}
		g.setFont(this.getFont());
		g.setColor(this.getFontColor());
		int imWidth = 0;
		if ( image != null) {
			imWidth = image.getWidth();
		}
		int internalWidth = imWidth + this.getFont().stringWidth(text) + 6;
		if(image !=null && text==null){
			g.drawImage(image,0,0,0);
			return;
		}
		if ( image == null && text != null) {
			if ( textAlign != -1 && internalWidth < getWidth()) {
				if ( textAlign == LEFT) {
					alignOffset = 0;
				}
				if ( textAlign == CENTRE) {
					alignOffset = (getWidth() - internalWidth) / 2;
				}
				if ( textAlign == RIGHT) {
					alignOffset = (getWidth() - internalWidth);
				}
				g.drawString(text, alignOffset, 0, Graphics.TOP | Graphics.LEFT);
			}
			else {
				Vector vector = getSubsection(this.getText(), Font.getDefaultFont(), getWidth(), " ,.?!");
				setHeight(vector.size() * gintLineHeight);
				for ( int i = gintBeginIndex ; i < vector.size() ; i++) {
					g.drawString((String) vector.elementAt(i), 0, gintLineHeight * (i - gintBeginIndex), 0);
					if ( (i - gintBeginIndex + 1) * gintLineHeight > getHeight()) break;
				}
				vector = null;
			}
			return;
		}
		if(image!=null && text!=null){
			g.drawImage(image,0,0,0);
			g.drawString(text, imWidth, 0, Graphics.TOP | Graphics.LEFT);
			return;
		}
		
	}

//	public void paint(Graphics g) {
		// for (int i = 0; i < skins.length; i++) {
		// getSkin(i).paint(this, g);
		// }
//		paintImpl(g);
//	}
	public Vector getSubsection(String strSource, Font font, int width, String strSplit) {
		vector = new Vector();
		String temp = strSource;
		int i, j;
		int LastLength = 1;
		int step = 0;
		try {
			while ( !temp.equals("")) {
				i = temp.indexOf("\n");
				if ( i > 0) {
					if ( font.stringWidth(temp.substring(0, i - 1)) >= width) {
						i = -1;
					}
				}
				if ( i == -1) {
					if ( LastLength > temp.length()) {
						i = temp.length();
					}
					else {
						i = LastLength;
						step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
						// 寰楀埌涓寸晫鐐

⌨️ 快捷键说明

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