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

📄 label.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 JAVA
字号:
/*
 * MWT - Micro Window Toolkit
 * Copyright (C) 2007 Lucas Domanico - lucazd@gmail.com
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://j2me-mwt.sourceforge.net/
 */

package mwt;


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

/**
 * <p>A label object is a component for displaying text or an image.</p>
 * 
 * <p>The text can be changed by the application, but the user cannot edit it directly.</p>
 */
public class Label extends Component {
	/** Constant value to get/set a skin/font. */
	static final public int STYLE_DEFAULT = 0;
	/** Constant value to get/set a skin/font. */
	static final public int STYLE_DISABLED = 1;

	private String text;
	private int textAlign;
	private final Font[] fonts;
	private Image image;
	
	final static private Font[] defaultFonts = {
		new Font(0,0,0,0),
		new Font(0x888888,0,0,0)
	};
	
	/** Creates a new text label. */
	public Label(int x, int y, int width, int height, String text) {
		super(x,y,width,height,false);
		fonts = new Font[] { defaultFonts[0].clone(), defaultFonts[1].clone() };
		this.setFocusable(false);
		this.text = text;
		this.image = null;
	}
	
	/** Creates a new image label. The initial width and height are defined by the given image size. */
	public Label(int x, int y, Image image) {
		super(x,y,image.getWidth(),image.getHeight(),false);
		this.setFocusable(false);
		fonts = new Font[] { defaultFonts[0].clone(), defaultFonts[1].clone() };
		this.text = "";
		this.image = image;
	}
	
	/** Gets the default font for the given style. */
	static final public Font getDefaultFont(int style) { return defaultFonts[style]; }
	/** Sets the default font for the given style. */
	static final public void setDefaultFont(int style, Font font) { defaultFonts[style] = font; }
	
	/** Gets this label font for the given style. */
	public Font getFont(int style) { return fonts[style]; }
	/** Sets this label font for the given style. */
	public void setFont(int style, Font font) { fonts[style] = font; }
	
	/** Gets this label's text. */ 
	public String getText() { return text; }
	/** Sets this label's text. */
	public void setText(String text) { this.text = text; }
	
	/** Gets the text align. A <a href="./Component.html#align">Component ALIGN constant</a>. */
	public int getTextAlign() { return textAlign; }
	/** Sets the text align. A <a href="./Component.html#align">Component ALIGN constant</a>. */
	public void setTextAlign(int textAlign) { this.textAlign = textAlign; }
	
	/** Gets the image, or null if this component is a text label. */
	public Image getImage() { return image; }
	/** Sets the image, or null if this component is a text label.
	 * 	@since 1.2 */
	final public void setImage(Image image) { this.image = image; }
	
	// overrides
	protected void paint(Graphics g, Window window) {
		if(image != null) {
			g.drawImage(image,0,0,0);
			return;
		}
		getFont(isHierarchyEnabled()? 0 : 1).write(g,text,0,0,getWidth(),getHeight(),textAlign);
	}
}

⌨️ 快捷键说明

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