📄 label.java
字号:
package com.jmobilecore.ui.core;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
/**
* Provides a text label for display on the screen. A label is not focusable.
*
* @author Greg Gridin
*/
public class Label extends Component {
/**
* The text of this label.
* @see #getText()
* @see #setText(String)
*/
protected String text;
/**
* Constructs a new label with the specified string of text,
* left justified.
* @param text the string that the label presents.
* A <code>null</code> value
* will be accepted without causing a NullPointerException
* to be thrown.
* The text can be single line only. If text contains
* \n characters (used to indicate line breaks) then it might overlap with other UI components.
* It is up to the caller to ensure correctness of input string
*/
public Label(String text) {
this(text, Style.TEXT_FONT, LEFT);
}
/**
* Constructs a new label that presents the specified string of
* text with the specified alignment.
* Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
* <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
* @param text the string that the label presents.
* A <code>null</code> value
* will be accepted without causing a NullPointerException
* to be thrown.
* The text can be single line only. If text contains
* \n characters (used to indicate line breaks) then it might overlap with other UI components.
* It is up to the caller to ensure correctness of input string
* @param alignment the alignment value.
*/
public Label(String text, int alignment) {
this(text, Style.TEXT_FONT, alignment);
}
/**
* Constructs a new label that presents the specified string of
* text with the specified alignment.
* Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
* <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
* @param text the string that the label presents.
* A <code>null</code> value
* will be accepted without causing a NullPointerException
* to be thrown.
* The text can be single line only. If text contains
* \n characters (used to indicate line breaks) then it might overlap with other UI components.
* It is up to the caller to ensure correctness of input string
* @param font the label font
* @param alignment the alignment value.
*/
public Label(String text, Font font, int alignment) {
this.font = font;
setText(text);
setFont(font);
this.alignment = alignment;
}
/**
* Paints the label to the screen.
*
* @param g The Graphics object to render to.
*/
public void paint(Graphics g) {
paintBackground(g);
prepareForeground(g);
int y = screenY + Style.V_GAP;
if (alignment == LEFT) {
g.drawString(text, Style.H_GAP, y, Graphics.TOP | Graphics.LEFT);
} else if (alignment == RIGHT) {
g.drawString(text, getWidth() - Style.H_GAP, y, Graphics.TOP | Graphics.RIGHT);
} else if (alignment == CENTER) {
g.drawString(text, getWidth() / 2, y, Graphics.TOP | Graphics.HCENTER);
}
super.paint(g);
}
/**
* Gets the text of this label.
* @return the text of this label, or <code>null</code> if
* the text has been set to <code>null</code>.
* @see #setText
*/
public String getText() {
return text;
}
/**
* Sets the text for this label to the specified text.
* @param text the text that this label displays.
* @see #getText
*/
public void setText(String text) {
this.text = text;
}
/**
* Creates title label
*
* @param titleText The title text
* @return title for current screen that could be uses with {@link ScreenCanvas#ScreenCanvas ScreenCanvas}
* @see Label
*/
static public Label createTitleLabel(String titleText) {
Label title = new Label(titleText, Style.HEADER_FONT, Label.CENTER);
title.background = Style.TITLE_BACKGROUND_COLOR;
title.foreground = Style.TITLE_TEXT_COLOR;
return title;
}
} // class Label
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -